<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="format-detection" content="telephone=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><link rel="icon" href="/blog/blog/images/icons/favicon-16x16.png?v=2.6.2" type="image/png" sizes="16x16"><link rel="icon" href="/blog/blog/images/icons/favicon-32x32.png?v=2.6.2" type="image/png" sizes="32x32"><meta property="og:type" content="website">
<meta property="og:title" content="斯人若彩虹 遇上方知有">
<meta property="og:url" content="https://bruce-jj.gitee.io/blog/page/5/index.html">
<meta property="og:site_name" content="斯人若彩虹 遇上方知有">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="何水浪">
<meta name="twitter:card" content="summary"><title>斯人若彩虹 遇上方知有</title><link ref="canonical" href="https://bruce-jj.gitee.io/blog/page/5/index.html"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.12.1/css/all.min.css" type="text/css"><link rel="stylesheet" href="/blog/css/index.css?v=2.6.2"><script>var Stun = window.Stun || {};
var CONFIG = {
  root: '/blog/',
  algolia: undefined,
  assistSearch: undefined,
  fontIcon: {"prompt":{"success":"fas fa-check-circle","info":"fas fa-arrow-circle-right","warning":"fas fa-exclamation-circle","error":"fas fa-times-circle"},"copyBtn":"fas fa-copy"},
  sidebar: {"offsetTop":"20px","tocMaxDepth":6},
  header: {"enable":true,"showOnPost":true,"scrollDownIcon":false},
  postWidget: {"endText":true},
  nightMode: {"enable":true},
  back2top: {"enable":true},
  codeblock: {"style":"default","highlight":"light","wordWrap":false},
  reward: false,
  fancybox: false,
  zoomImage: {"gapAside":"20px"},
  galleryWaterfall: undefined,
  lazyload: false,
  pjax: undefined,
  externalLink: {"icon":{"enable":true,"name":"fas fa-external-link-alt"}},
  shortcuts: undefined,
  prompt: {"copyButton":"复制","copySuccess":"复制成功","copyError":"复制失败"},
  sourcePath: {"js":"js","css":"css","images":"images"},
};

window.CONFIG = CONFIG;</script><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/blog/atom.xml" title="斯人若彩虹 遇上方知有" type="application/atom+xml">
</head><body><div class="container" id="container"><header class="header" id="header"><div class="header-inner"><nav class="header-nav header-nav--fixed"><div class="header-nav-inner"><div class="header-nav-menubtn"><i class="fas fa-bars"></i></div><div class="header-nav-menu"><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/"><span class="header-nav-menu-item__icon"><i class="fas fa-home"></i></span><span class="header-nav-menu-item__text">首页</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/archives/"><span class="header-nav-menu-item__icon"><i class="fas fa-folder-open"></i></span><span class="header-nav-menu-item__text">归档</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/categories/"><span class="header-nav-menu-item__icon"><i class="fas fa-layer-group"></i></span><span class="header-nav-menu-item__text">分类</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/tags/"><span class="header-nav-menu-item__icon"><i class="fas fa-tags"></i></span><span class="header-nav-menu-item__text">标签</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/friends/"><span class="header-nav-menu-item__icon"><i class="fas fa-link"></i></span><span class="header-nav-menu-item__text">友链</span></a></div></div><div class="header-nav-search"><span class="header-nav-search__icon"><i class="fas fa-search"></i></span><span class="header-nav-search__text">搜索</span></div><div class="header-nav-mode"><div class="mode"><div class="mode-track"><span class="mode-track-moon"></span><span class="mode-track-sun"></span></div><div class="mode-thumb"></div></div></div></div></nav><div class="header-banner"><div class="header-banner-info"><div class="header-banner-info__title">斯人若彩虹 遇上方知有</div><div class="header-banner-info__subtitle"></div></div></div></div></header><main class="main" id="main"><div class="main-inner"><div class="content-wrap" id="content-wrap"><div class="content content-home" id="content"><section class="postlist"><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/blog/2021/03/11/PYTHON%20%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E7%B2%BE%E9%80%9A-%E5%88%A9%E7%94%A8selenium%20%E5%A6%82%E4%BD%95%E7%88%AC%E5%8F%96%E7%BD%91%E7%AB%99%E6%95%B0%E6%8D%AE/">PYTHON 从入门到精通-利用selenium 如何爬取网站数据</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-11</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-03-15</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">387</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">4分</span></span></div></header><div class="post-body"><div class="post-excerpt"><!-- 页面必备样式 -->
<style>
.back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;}
.back-top>a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height: 50px;border-radius: 50px;background-color:rgba(255,255,255,0.5);}
</style>
<div class="back-top">
    <a href="#top"> >返回顶部< </a>
</div>
<!-- toc -->
<!-- 必备样式结束 -->


        <h1 id="一、爬取豆瓣电影td数据代码"   >
          <a href="#一、爬取豆瓣电影td数据代码" class="heading-link"><i class="fas fa-link"></i></a><a href="#一、爬取豆瓣电影td数据代码" class="headerlink" title="一、爬取豆瓣电影td数据代码"></a>一、爬取豆瓣电影td数据代码</h1>
      <figure class="highlight py"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#-*- coding:UTF-8 -*-</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># selenium对豆瓣页面进行操作</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">print_hi</span>(<span class="params">name</span>):</span></span><br><span class="line">    <span class="comment"># Use a breakpoint in the code line below to debug your script.</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&#x27;Hi, <span class="subst">&#123;name&#125;</span>&#x27;</span>)  <span class="comment"># Press Ctrl+F8 to toggle the breakpoint.</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># Press the green button in the gutter to run the script.</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#Main函数入口</span></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    browser = webdriver.Chrome()</span><br><span class="line">    browser.get(<span class="string">&#x27;https://movie.douban.com/&#x27;</span>)</span><br><span class="line">    tagname = browser.find_elements_by_tag_name(<span class="string">&#x27;td&#x27;</span>) </span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> item <span class="keyword">in</span> tagname:</span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            <span class="built_in">int</span>(item.text) <span class="comment"># 这一句出现异常时</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">except</span> Exception:</span><br><span class="line">            <span class="built_in">print</span>(item.text) <span class="comment"># 将会走这一步</span></span><br><span class="line"></span><br></pre></td></tr></table></div></figure>



        <h1 id="二、爬取豆瓣电影-轮播图中数据的href数据"   >
          <a href="#二、爬取豆瓣电影-轮播图中数据的href数据" class="heading-link"><i class="fas fa-link"></i></a><a href="#二、爬取豆瓣电影-轮播图中数据的href数据" class="headerlink" title="二、爬取豆瓣电影-轮播图中数据的href数据"></a>二、爬取豆瓣电影-轮播图中数据的href数据</h1>
      <figure class="highlight py"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#找到相应的轮播图数据的href</span></span><br><span class="line"><span class="comment"># This is a sample Python script.</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Press Shift+F10 to execute it or replace it with your code.</span></span><br><span class="line"><span class="comment"># Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">print_hi</span>(<span class="params">name</span>):</span></span><br><span class="line">    <span class="comment"># Use a breakpoint in the code line below to debug your script.</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&#x27;Hi, <span class="subst">&#123;name&#125;</span>&#x27;</span>)  <span class="comment"># Press Ctrl+F8 to toggle the breakpoint.</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># Press the green button in the gutter to run the script.</span></span><br><span class="line"><span class="comment">#Main函数入口</span></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    browser = webdriver.Chrome()</span><br><span class="line">    browser.get(<span class="string">&#x27;https://movie.douban.com/&#x27;</span>)</span><br><span class="line">    tagnames = browser.find_elements_by_class_name(<span class="string">&#x27;list-wp&#x27;</span>) <span class="comment"># 找到class为cover-wp所在的标签</span></span><br><span class="line">    <span class="keyword">for</span> container <span class="keyword">in</span> tagnames:</span><br><span class="line">        items = container.find_elements_by_class_name(<span class="string">&#x27;slide-page&#x27;</span>) <span class="comment"># 所有的slide-page</span></span><br><span class="line">        <span class="keyword">for</span> item <span class="keyword">in</span> items:</span><br><span class="line">            tag = item.find_elements_by_tag_name(<span class="string">&#x27;a&#x27;</span>) <span class="comment"># 找到每一个slide-page的a标签</span></span><br><span class="line">            <span class="built_in">print</span>(<span class="built_in">len</span>(tag))</span><br><span class="line">            <span class="keyword">for</span> Tag <span class="keyword">in</span> tag:                             <span class="comment"># 每一个a标签的 href属性</span></span><br><span class="line">                <span class="built_in">print</span>(Tag.get_property(<span class="string">&#x27;href&#x27;</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment"># See PyCharm help at https://www.jetbrains.com/help/pycharm/</span></span><br></pre></td></tr></table></div></figure>

</div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/blog/2021/03/11/PYTHON%20%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E7%B2%BE%E9%80%9A-%E7%B1%BBClass/">PYTHON 从入门到精通-类Class</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-11</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-03-15</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">292</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">2分</span></span></div></header><div class="post-body"><div class="post-excerpt"><!-- 页面必备样式 -->
<style>
.back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;}
.back-top>a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height: 50px;border-radius: 50px;background-color:rgba(255,255,255,0.5);}
</style>
<div class="back-top">
    <a href="#top"> >返回顶部< </a>
</div>
<!-- toc -->
<!-- 必备样式结束 -->


        <h1 id="类的定义："   >
          <a href="#类的定义：" class="heading-link"><i class="fas fa-link"></i></a><a href="#类的定义：" class="headerlink" title="类的定义："></a>类的定义：</h1>
      <figure class="highlight py"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CLASS_NAME</span>:</span></span><br><span class="line">	attr1 = <span class="number">0</span></span><br><span class="line">	<span class="comment"># 构造函数__init__</span></span><br><span class="line">	<span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, param1, param2...</span>):</span> <span class="comment"># self相当于C++中的this对象</span></span><br><span class="line">		self.param1 = param1</span><br><span class="line">		...</span><br><span class="line">	</span><br><span class="line">	<span class="comment"># 成员函数 将变量attr1加一</span></span><br><span class="line">	<span class="function"><span class="keyword">def</span> <span class="title">fun</span>(<span class="params">self</span>):</span></span><br><span class="line">		attr1 += <span class="number">1</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment"># 类方法 调用时直接用CLASSNAME.set_attr_for_all</span></span><br><span class="line"><span class="meta">	@classmethod</span></span><br><span class="line">	<span class="function"><span class="keyword">def</span> <span class="title">set_attr_for_all</span>(<span class="params">cls, attr1</span>):</span></span><br><span class="line">		cls.attr1 = attr1</span><br><span class="line">	</span><br><span class="line">	<span class="comment"># 静态方法 一般写不涉及到某个对象 或者类的相关属性或者方法</span></span><br><span class="line"><span class="meta">	@staticmethod</span></span><br><span class="line">	<span class="function"><span class="keyword">def</span> <span class="title">hello_class</span>():</span></span><br><span class="line">		<span class="built_in">print</span>(<span class="string">&#x27;Hello World!&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建并初始化对象</span></span><br><span class="line">class1 = CLASS_NAME()</span><br><span class="line"></span><br><span class="line"><span class="comment"># class1.xxx 代表对象实例的变量 或者 方法</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># CLASS_NAME.xxx 代表类的变量或者方法</span></span><br><span class="line">	</span><br></pre></td></tr></table></div></figure>


        <h1 id="类的继承"   >
          <a href="#类的继承" class="heading-link"><i class="fas fa-link"></i></a><a href="#类的继承" class="headerlink" title="类的继承"></a>类的继承</h1>
      <p>
    比如有一个类：Human，另外有一个Student类
    现在用Student类继承于Human 
</p>

<figure class="highlight py"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Human类的构造函数：</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Human</span>:</span></span><br><span class="line">	<span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name</span>):</span></span><br><span class="line">		self.name = name</span><br></pre></td></tr></table></div></figure>

<figure class="highlight py"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Student比Human类多一个属性studentnum </span></span><br><span class="line"><span class="comment"># 而从父类继承来的name属性 可以通过父类的构造方法进行构造</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span>(<span class="params">Human</span>):</span></span><br><span class="line">	<span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name, studentnum</span>):</span></span><br><span class="line">	</span><br><span class="line">		<span class="comment"># 利用父类的构造方法 构造name属性</span></span><br><span class="line">		<span class="built_in">super</span>.<span class="keyword">class</span>().__init__(name)</span><br><span class="line">		self.studentnum = studentnum</span><br></pre></td></tr></table></div></figure></div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/blog/2021/03/11/%E4%BB%8E%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%88%9B%E5%BB%BASpringboot%20+%20Thymelear%20+%20layui%E9%A1%B9%E7%9B%AE/">从零基础创建Springboot_Thymelear_layui项目</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-11</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-05-02</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">289</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">2分</span></span></div></header><div class="post-body"><div class="post-excerpt"><!-- 页面必备样式 -->
<style>
.back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;}
.back-top>a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height: 50px;border-radius: 50px;background-color:rgba(255,255,255,0.5);}
</style>
<div class="back-top">
    <a href="#top"> >返回顶部< </a>
</div>
<!-- toc -->
<!-- 必备样式结束 -->


        <h1 id="第一步：打开idea创建项目"   >
          <a href="#第一步：打开idea创建项目" class="heading-link"><i class="fas fa-link"></i></a><a href="#第一步：打开idea创建项目" class="headerlink" title="第一步：打开idea创建项目"></a>第一步：打开idea创建项目</h1>
      <p><img   src="https://img-blog.csdnimg.cn/20210109170230558.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"><br>直接点next下一步；</p>
<p><img   src="https://img-blog.csdnimg.cn/20210109170455100.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"><br>next点完，依赖后面再添加。</p>
<p><mark>依赖删除，重新添加</mark><br>    ①：spring-boot-starter-web<br>    ②：spring-boot-starter-thymeleaf</p>

        <h2 id="创建默认页面static和templates文件夹"   >
          <a href="#创建默认页面static和templates文件夹" class="heading-link"><i class="fas fa-link"></i></a><a href="#创建默认页面static和templates文件夹" class="headerlink" title="创建默认页面static和templates文件夹"></a>创建默认页面static和templates文件夹</h2>
      <p>1、其中 static 放除pages以外的<br>2、templates 放pages<br>（数据来自ok-admin-master) 百度网盘有</p>
<p><img   src="https://img-blog.csdnimg.cn/20210109171428197.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"><br>存入之后，项目结构图为（记得加入index.html)<br><img   src="https://img-blog.csdnimg.cn/20210109171502442.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>

        <h3 id="首先测试主页，编写controller访问index（涉及SpringMVC）"   >
          <a href="#首先测试主页，编写controller访问index（涉及SpringMVC）" class="heading-link"><i class="fas fa-link"></i></a><a href="#首先测试主页，编写controller访问index（涉及SpringMVC）" class="headerlink" title="首先测试主页，编写controller访问index（涉及SpringMVC）"></a>首先测试主页，编写controller访问index（涉及SpringMVC）</h3>
      <p>注意，启动时可能会报相应Test错误（由于改动了相应的依赖）</p>
<p><img   src="https://img-blog.csdnimg.cn/20210109172033900.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"><br>只需要删除相应Test文件即可<br><img   src="https://img-blog.csdnimg.cn/20210109172127323.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>
<p>编写相应的Controller 写页面跳转即可<br>    <img   src="https://img-blog.csdnimg.cn/20210109173707132.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>

        <h2 id="前端若iframe嵌套页面为404"   >
          <a href="#前端若iframe嵌套页面为404" class="heading-link"><i class="fas fa-link"></i></a><a href="#前端若iframe嵌套页面为404" class="headerlink" title="前端若iframe嵌套页面为404"></a>前端若iframe嵌套页面为404</h2>
      <p>则比如：iframe标签中，src属性为welcome<br>那么写一个Controller RequestMapping参数为”welcome” 直接返回相应的前端页面即可 避免404<br>看图：</p>
<p><img src="https://img-blog.csdnimg.cn/20210109175845496.png" alt="在这里插入图片描述"><br><img src="https://img-blog.csdnimg.cn/20210109175856423.png" alt="在这里插入图片描述"></p>

        <h2 id="一级菜单与二级菜单-在menu-json中"   >
          <a href="#一级菜单与二级菜单-在menu-json中" class="heading-link"><i class="fas fa-link"></i></a><a href="#一级菜单与二级菜单-在menu-json中" class="headerlink" title="一级菜单与二级菜单 在menu.json中"></a>一级菜单与二级菜单 在menu.json中</h2>
      <p><img   src="https://img-blog.csdnimg.cn/20210109174132507.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"><br>大致页面如下：</p>
<p><strong>localhost:8080</strong> 或者 <strong>localhost:8080/index</strong> （默认欢迎页面为Index）</p>
<p><img   src="https://img-blog.csdnimg.cn/20210109180025740.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>
</div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/blog/2021/03/11/%E7%9B%AE%E5%BD%95%E6%B5%8B%E8%AF%95/">目录测试</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-11</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-05-06</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">51</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">1分</span></span></div></header><div class="post-body"><div class="post-excerpt">
        <h1 id="第一步：下载插件（HEXO-TOC）"   >
          <a href="#第一步：下载插件（HEXO-TOC）" class="heading-link"><i class="fas fa-link"></i></a><a href="#第一步：下载插件（HEXO-TOC）" class="headerlink" title="第一步：下载插件（HEXO-TOC）"></a>第一步：下载插件（HEXO-TOC）</h1>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cnpm install hexo-toc --save</span><br></pre></td></tr></table></div></figure>


        <h1 id="第二步：配置博客根目录下的-config-yml文件"   >
          <a href="#第二步：配置博客根目录下的-config-yml文件" class="heading-link"><i class="fas fa-link"></i></a><a href="#第二步：配置博客根目录下的-config-yml文件" class="headerlink" title="第二步：配置博客根目录下的_config.yml文件"></a>第二步：配置博客根目录下的_config.yml文件</h1>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">toc:</span><br><span class="line">   maxdepth: <span class="number">3</span></span><br></pre></td></tr></table></div></figure>


        <h1 id="第三步：在文章相应位置引用目录"   >
          <a href="#第三步：在文章相应位置引用目录" class="heading-link"><i class="fas fa-link"></i></a><a href="#第三步：在文章相应位置引用目录" class="headerlink" title="第三步：在文章相应位置引用目录"></a>第三步：在文章相应位置引用目录</h1>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- toc --&gt;</span><br></pre></td></tr></table></div></figure></div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/blog/2021/03/11/Mybatis/">Mybatis学习笔记(所有代码FROM SIKI学院)</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-11</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-05-08</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">4.9k</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">45分</span></span></div></header><div class="post-body"><div class="post-excerpt"><!-- 页面必备样式 -->
<style>
.back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;}
.back-top>a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height: 50px;border-radius: 50px;background-color:rgba(255,255,255,0.5);}
</style>
<div class="back-top">
    <a href="#top"> >返回顶部< </a>
</div>
<!-- toc -->
<!-- 必备样式结束 -->



        <h3 id="Mybatis是什么？"   >
          <a href="#Mybatis是什么？" class="heading-link"><i class="fas fa-link"></i></a><a href="#Mybatis是什么？" class="headerlink" title="Mybatis是什么？"></a>Mybatis是什么？</h3>
      <p>
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code，
并且改名为MyBatis 。2013年11月迁移到Github。MyBatis是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。
MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以对配置和原生Map使用简单的 XML 或注解，将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。总而言之MyBatis是一个轻量级简化数据库操作的框架。
</p>


        <h3 id="Mybatis架构"   >
          <a href="#Mybatis架构" class="heading-link"><i class="fas fa-link"></i></a><a href="#Mybatis架构" class="headerlink" title="Mybatis架构"></a>Mybatis架构</h3>
      <p><img   src="https://img-blog.csdnimg.cn/20210313165831616.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>

        <h3 id="Mybatis的配置文件（包括数据库连接，MapperXML配置文件位置）"   >
          <a href="#Mybatis的配置文件（包括数据库连接，MapperXML配置文件位置）" class="heading-link"><i class="fas fa-link"></i></a><a href="#Mybatis的配置文件（包括数据库连接，MapperXML配置文件位置）" class="headerlink" title="Mybatis的配置文件（包括数据库连接，MapperXML配置文件位置）"></a>Mybatis的配置文件（包括数据库连接，MapperXML配置文件位置）</h3>
      <figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">configuration</span></span></span><br><span class="line"><span class="meta">  <span class="meta-keyword">PUBLIC</span> <span class="meta-string">&quot;-//mybatis.org//DTD Config 3.0//EN&quot;</span></span></span><br><span class="line"><span class="meta">  <span class="meta-string">&quot;http://mybatis.org/dtd/mybatis-3-config.dtd&quot;</span>&gt;</span></span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 读取数据库连接的配置文件 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">properties</span> <span class="attr">resource</span>=<span class="string">&quot;db.properties&quot;</span>/&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!--    推荐使用package包的形式来配置别名</span></span><br><span class="line"><span class="comment">			包的形式会扫描主包及子包下的所有文件</span></span><br><span class="line"><span class="comment">			以对象类名为别名，大小写不限，推荐使用小写</span></span><br><span class="line"><span class="comment">	 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">typeAliases</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- 	&lt;typeAlias type=&quot;com.sikiedu.bean.User&quot; alias=&quot;user&quot;/&gt; --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">package</span> <span class="attr">name</span>=<span class="string">&quot;com.sikiedu.bean&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">typeAliases</span>&gt;</span></span><br><span class="line">	</span><br><span class="line"> <span class="comment">&lt;!-- 在集成spring 不用environments这个property --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">environments</span> <span class="attr">default</span>=<span class="string">&quot;development&quot;</span>&gt;</span></span><br><span class="line">	  </span><br><span class="line">    <span class="tag">&lt;<span class="name">environment</span> <span class="attr">id</span>=<span class="string">&quot;development&quot;</span>&gt;</span></span><br><span class="line">		</span><br><span class="line">    <span class="comment">&lt;!-- 使用jdbc的事务 --&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">&quot;JDBC&quot;</span>/&gt;</span></span><br><span class="line">	  </span><br><span class="line">      <span class="comment">&lt;!-- 使用连接池 连接数据库 --&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">&quot;POOLED&quot;</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- 注意下面的jdbc.driver在文件db.properties中: --&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- 写法是：jdbc.driver=com.mysql.jdbc.Driver --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;driver&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.driver&#125;&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;url&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.url&#125;&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;username&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.username&#125;&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.password&#125;&quot;</span>/&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">dataSource</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">environment</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">environments</span>&gt;</span></span><br><span class="line">  </span><br><span class="line">  <span class="comment">&lt;!-- 配置映射器配置文件 XXX.xml 的位置 --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">mappers</span>&gt;</span></span><br><span class="line">   	<span class="comment">&lt;!-- &lt;mapper resource=&quot;mapper/UserMapper.xml&quot;/&gt; --&gt;</span></span><br><span class="line">   	<span class="comment">&lt;!-- &lt;mapper url=&quot;file:\\\D:\Work\Siki_edu\JavaEE\Teach\SSM框架\ssm_project\ssm_mybatis\src\mapper\UserMapper.xml&quot;/&gt; --&gt;</span></span><br><span class="line">   	<span class="comment">&lt;!-- &lt;mapper class=&quot;com.sikiedu.mapper.UserMapper&quot;/&gt; --&gt;</span></span><br><span class="line">   	<span class="comment">&lt;!-- 推荐使用包的形式配置 --&gt;</span></span><br><span class="line">   	<span class="tag">&lt;<span class="name">package</span> <span class="attr">name</span>=<span class="string">&quot;com.sikiedu.mapper&quot;</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">mappers</span>&gt;</span></span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br></pre></td></tr></table></div></figure>
<p><strong>注意：</strong><br>当使用<code>&lt;package/&gt;</code>标签时 <strong>映射接口文件与映射配置文件共同应该在Mapper文件夹下</strong></p>
<p>否则使用mapper标签 且用resource指定对应的配置文件所在目录</p>

        <h3 id="Mybatis某个映射接口文件以及对应的XML文件（比如UserMapper-java-amp-UserMapper-xml"   >
          <a href="#Mybatis某个映射接口文件以及对应的XML文件（比如UserMapper-java-amp-UserMapper-xml" class="heading-link"><i class="fas fa-link"></i></a><a href="#Mybatis某个映射接口文件以及对应的XML文件（比如UserMapper-java-amp-UserMapper-xml" class="headerlink" title="Mybatis某个映射接口文件以及对应的XML文件（比如UserMapper.java &amp; UserMapper.xml)"></a>Mybatis某个映射接口文件以及对应的XML文件（比如UserMapper.java &amp; UserMapper.xml)</h3>
      <figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">mapper</span></span></span><br><span class="line"><span class="meta">  <span class="meta-keyword">PUBLIC</span> <span class="meta-string">&quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;</span></span></span><br><span class="line"><span class="meta">  <span class="meta-string">&quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 注意域名namespace不要写错 相当于自己地址 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">&quot;com.sikiedu.mapper.UserMapper&quot;</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="tag">&lt;<span class="name">sql</span> <span class="attr">id</span>=<span class="string">&quot;myselect&quot;</span>&gt;</span></span><br><span class="line">		SELECT * </span><br><span class="line">		FROM user</span><br><span class="line">	<span class="tag">&lt;/<span class="name">sql</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">&lt;!-- 根据id查询用户 --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- 注意! 标签中的id属性值,必须与对应Mapper接口文件中的方法名一致!!! --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectUserById&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;Integer&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;user&quot;</span>&gt;</span></span><br><span class="line">		 select * from user where u_id = #&#123;id&#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 根据名字模糊查询用户 --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- #&#123;&#125; 占位符 尽量选用#&#123;&#125;来解决问题 &#x27;1&#x27; &#x27;王&#x27;--&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- $&#123;&#125; 字符串拼接 or 1=1  王 --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- order by $&#123;&#125; --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectUserByName&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;String&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;com.sikiedu.bean.User&quot;</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- SELECT * FROM USER WHERE u_username LIKE &#x27;%$&#123;value&#125;%&#x27; --&gt;</span></span><br><span class="line">		SELECT * FROM USER WHERE u_username LIKE &quot;%&quot;#&#123;name&#125;&quot;%&quot;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 添加用户 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">&quot;insertUser&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;com.sikiedu.bean.User&quot;</span>&gt;</span></span><br><span class="line">		insert into user values( null, #&#123;u_username&#125; , #&#123;u_password&#125;, #&#123;u_sex&#125;, #&#123;u_createTime&#125;, #&#123;u_cid&#125; )</span><br><span class="line">	<span class="tag">&lt;/<span class="name">insert</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 修改用户 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">update</span> <span class="attr">id</span>=<span class="string">&quot;updateUser&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;com.sikiedu.bean.User&quot;</span> &gt;</span></span><br><span class="line">		update user set u_username = #&#123;u_username&#125; where u_id = #&#123;u_id&#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">update</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 根据id删除用户 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">delete</span> <span class="attr">id</span>=<span class="string">&quot;deleteUserById&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;Integer&quot;</span>&gt;</span></span><br><span class="line">		delete from user Where u_id = #&#123;id&#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">delete</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectUserByUserVoId&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;UserVo&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;user&quot;</span>&gt;</span></span><br><span class="line">		 select * from user where u_id = #&#123;user.u_id&#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 	public Integer selectUserCount(); --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectUserCount&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;Integer&quot;</span>&gt;</span></span><br><span class="line">		Select Count(*) from user</span><br><span class="line">	<span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">	</span><br><span class="line"><span class="comment">&lt;!-- public List&lt;UserVo&gt; selectAllUserVo(); //查询所有用户包装类 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">type</span>=<span class="string">&quot;UserVo&quot;</span> <span class="attr">id</span>=<span class="string">&quot;uservolist&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">&quot;u_id&quot;</span> <span class="attr">column</span>=<span class="string">&quot;u_id&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;u_username&quot;</span> <span class="attr">column</span>=<span class="string">&quot;u_username&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;u_sex&quot;</span> <span class="attr">column</span>=<span class="string">&quot;u_sex&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">association</span> <span class="attr">property</span>=<span class="string">&quot;country&quot;</span> <span class="attr">javaType</span>=<span class="string">&quot;Country&quot;</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">&quot;id&quot;</span> <span class="attr">column</span>=<span class="string">&quot;c_id&quot;</span>/&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;c_countryname&quot;</span> <span class="attr">column</span>=<span class="string">&quot;c_countryname&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">association</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectAllUserVo&quot;</span> <span class="attr">resultMap</span>=<span class="string">&quot;uservolist&quot;</span>&gt;</span></span><br><span class="line">		SELECT </span><br><span class="line">		u.`u_id`, </span><br><span class="line">		u.`u_username`,</span><br><span class="line">		u.`u_sex`, </span><br><span class="line">		c.`c_id`, </span><br><span class="line">		c.`c_countryname` </span><br><span class="line">		FROM USER u </span><br><span class="line">		LEFT JOIN </span><br><span class="line">		country c </span><br><span class="line">		ON u.`u_cid`=c.`c_id`</span><br><span class="line">	<span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 	//多条件查询 通过用户对象中的条件查询用户列表</span></span><br><span class="line"><span class="comment">	public List&lt;User&gt; selectUserListByUser(User u); --&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectUserListByUser&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;User&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;User&quot;</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- 查询用户性别 模糊查询用户名 查询用户cid 国籍ID --&gt;</span></span><br><span class="line">		SELECT * </span><br><span class="line">		FROM user</span><br><span class="line">		<span class="tag">&lt;<span class="name">where</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- where标签可以去掉开头的AND --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;u_sex != null and u_sex != &#x27;&#x27;&quot;</span>&gt;</span></span><br><span class="line">			 AND u_sex = #&#123;u_sex&#125; </span><br><span class="line">		<span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;u_username != null and u_username != &#x27;&#x27;&quot;</span>&gt;</span></span><br><span class="line">			AND u_username like &quot;%&quot;#&#123;u_username&#125;&quot;%&quot; </span><br><span class="line">		<span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;u_cid != null&quot;</span>&gt;</span></span><br><span class="line">			AND u_cid = #&#123;u_cid&#125;</span><br><span class="line">		<span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">where</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectUserListByUserTrim&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;User&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;User&quot;</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- 查询用户性别 模糊查询用户名 查询用户cid 国籍ID --&gt;</span></span><br><span class="line">		SELECT * </span><br><span class="line">		FROM user</span><br><span class="line">		<span class="tag">&lt;<span class="name">trim</span> <span class="attr">prefix</span>=<span class="string">&quot;where&quot;</span> <span class="attr">suffixOverrides</span>=<span class="string">&quot;AND&quot;</span>&gt;</span></span><br><span class="line">			<span class="comment">&lt;!-- where标签可以去掉开头的AND --&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;u_sex != null and u_sex != &#x27;&#x27;&quot;</span>&gt;</span></span><br><span class="line">				 u_sex = #&#123;u_sex&#125; AND </span><br><span class="line">			<span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;u_username != null and u_username != &#x27;&#x27;&quot;</span>&gt;</span></span><br><span class="line">				u_username like &quot;%&quot;#&#123;u_username&#125;&quot;%&quot; AND </span><br><span class="line">			<span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;u_cid != null&quot;</span>&gt;</span></span><br><span class="line">				 u_cid = #&#123;u_cid&#125; AND</span><br><span class="line">			<span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">trim</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- public void updateSetUser(User u); --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">update</span> <span class="attr">id</span>=<span class="string">&quot;updateSetUser&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;User&quot;</span>&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- 用户名 用户密码 性别 用id来限制 --&gt;</span></span><br><span class="line">		update user </span><br><span class="line">		<span class="tag">&lt;<span class="name">set</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;u_username != null and u_username != &#x27;&#x27;&quot;</span>&gt;</span></span><br><span class="line">				u_username = #&#123;u_username&#125;,</span><br><span class="line">			<span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;u_password != null and u_password != &#x27;&#x27;&quot;</span>&gt;</span></span><br><span class="line">				u_password = #&#123;u_password&#125;,</span><br><span class="line">			<span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;u_sex != null and u_sex != &#x27;&#x27;&quot;</span>&gt;</span></span><br><span class="line">				u_sex = #&#123;u_sex&#125;</span><br><span class="line">			<span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">set</span>&gt;</span></span><br><span class="line">		Where u_id = #&#123;u_id&#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">update</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 	//使用多个id获取用户列表</span></span><br><span class="line"><span class="comment">	public List&lt;User&gt; selectUserListByIds(); (1, 3, 5)--&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectUserListByIds&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;User&quot;</span>&gt;</span></span><br><span class="line">		SELECT * </span><br><span class="line">		FROM USER </span><br><span class="line">		WHERE u_id </span><br><span class="line">		IN </span><br><span class="line">		<span class="comment">&lt;!-- (1,2,3) --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">foreach</span> <span class="attr">collection</span>=<span class="string">&quot;array&quot;</span> <span class="attr">item</span>=<span class="string">&quot;id&quot;</span> <span class="attr">open</span>=<span class="string">&quot;(&quot;</span> <span class="attr">close</span>=<span class="string">&quot;)&quot;</span> <span class="attr">separator</span>=<span class="string">&quot;,&quot;</span>&gt;</span></span><br><span class="line">			#&#123;id&#125; </span><br><span class="line">		<span class="tag">&lt;/<span class="name">foreach</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">		<span class="comment">&lt;!-- 	//使用多个id获取用户列表</span></span><br><span class="line"><span class="comment">	public List&lt;User&gt; selectUserListByIds(); (1, 3, 5)--&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectUserListByList&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;User&quot;</span>&gt;</span></span><br><span class="line">		SELECT * </span><br><span class="line">		FROM USER </span><br><span class="line">		WHERE u_id </span><br><span class="line">		IN </span><br><span class="line">		<span class="comment">&lt;!-- (1,2,3) --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">foreach</span> <span class="attr">collection</span>=<span class="string">&quot;list&quot;</span> <span class="attr">item</span>=<span class="string">&quot;id&quot;</span> <span class="attr">open</span>=<span class="string">&quot;(&quot;</span> <span class="attr">close</span>=<span class="string">&quot;)&quot;</span> <span class="attr">separator</span>=<span class="string">&quot;,&quot;</span>&gt;</span></span><br><span class="line">			#&#123;id&#125; </span><br><span class="line">		<span class="tag">&lt;/<span class="name">foreach</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 	List&lt;User&gt; selectUserByVo(CountryVo vo); --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectUserByVo&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;CountryVo&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;User&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">include</span> <span class="attr">refid</span>=<span class="string">&quot;myselect&quot;</span>/&gt;</span></span><br><span class="line">		WHERE u_id </span><br><span class="line">		IN </span><br><span class="line">		<span class="comment">&lt;!-- (1,2,3) --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">foreach</span> <span class="attr">collection</span>=<span class="string">&quot;list&quot;</span> <span class="attr">item</span>=<span class="string">&quot;id&quot;</span> <span class="attr">open</span>=<span class="string">&quot;(&quot;</span> <span class="attr">close</span>=<span class="string">&quot;)&quot;</span> <span class="attr">separator</span>=<span class="string">&quot;,&quot;</span>&gt;</span></span><br><span class="line">			#&#123;id&#125; </span><br><span class="line">		<span class="tag">&lt;/<span class="name">foreach</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 	//使用多个id获取用户列表 by userVO</span></span><br><span class="line"><span class="comment">	public List&lt;User&gt; selectUserListByUserVo(UserVo vo); --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectUserListByUserVo&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;UserVo&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;User&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">include</span> <span class="attr">refid</span>=<span class="string">&quot;myselect&quot;</span>/&gt;</span></span><br><span class="line">		WHERE u_id </span><br><span class="line">		IN </span><br><span class="line">		<span class="comment">&lt;!-- (1,2,3) --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">foreach</span> <span class="attr">collection</span>=<span class="string">&quot;idList&quot;</span> <span class="attr">item</span>=<span class="string">&quot;id&quot;</span> <span class="attr">open</span>=<span class="string">&quot;(&quot;</span> <span class="attr">close</span>=<span class="string">&quot;)&quot;</span> <span class="attr">separator</span>=<span class="string">&quot;,&quot;</span>&gt;</span></span><br><span class="line">			#&#123;id&#125; </span><br><span class="line">		<span class="tag">&lt;/<span class="name">foreach</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line"><span class="tag">&lt;/<span class="name">mapper</span>&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h3 id="在Mybatis中的（XXXMapper-xml）映射文件中需要注意的事项"   >
          <a href="#在Mybatis中的（XXXMapper-xml）映射文件中需要注意的事项" class="heading-link"><i class="fas fa-link"></i></a><a href="#在Mybatis中的（XXXMapper-xml）映射文件中需要注意的事项" class="headerlink" title="在Mybatis中的（XXXMapper.xml）映射文件中需要注意的事项"></a>在Mybatis中的（XXXMapper.xml）映射文件中需要注意的事项</h3>
      <ol>
<li>namespace 代表该映射文件的域名地址 不要填错</li>
<li>select标签中的id <strong>必须与对应Mapper.java文件中的方法名一致！！！</strong></li>
<li>parameterType 代表接口中方法的形参类型 要一致</li>
<li>resultType代表返回类型，以下两者均可 要一致<br>（com.sikiedu.bean.User是全称）<br>（user是Mybatis配置文件中配置好的 TypeAlias起的别名）</li>
<li>具体的SQL语句中 #{x} 的x代表UserMapper接口文件中形参名 要一致</li>
</ol>

        <h3 id="UserMapper-java文件（映射接口文件）"   >
          <a href="#UserMapper-java文件（映射接口文件）" class="heading-link"><i class="fas fa-link"></i></a><a href="#UserMapper-java文件（映射接口文件）" class="headerlink" title="UserMapper.java文件（映射接口文件）"></a>UserMapper.java文件（映射接口文件）</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.sikiedu.mapper;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.sikiedu.bean.User;</span><br><span class="line"><span class="keyword">import</span> com.sikiedu.bean.UserVo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserMapper</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//mapper动态代理开发四大原则 + 一个注意</span></span><br><span class="line">	<span class="comment">//1、接口方法名需要与mapper.xml的要调用的sql语句的id一致</span></span><br><span class="line">	<span class="comment">//2、接口的形参类型需要与mapper.xml parameterType 一致</span></span><br><span class="line">	<span class="comment">//3、接口的返回值需要与mapper.xml resultType一致</span></span><br><span class="line">	<span class="comment">//4、mapper.xml 中namespace要与接口的全包名一致</span></span><br><span class="line">	<span class="comment">//5、注意mapper动态代理开发中，根据返回值类型来自动选择</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">//通过id查询一个用户</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> User <span class="title">selectUserById</span><span class="params">(Integer id)</span></span>;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//通过用户名模糊查询 获取用户列表</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> List&lt;User&gt; <span class="title">selectUserByName</span><span class="params">(String name)</span></span>;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//通过UserVo id查询一个用户</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> User <span class="title">selectUserByUserVoId</span><span class="params">(UserVo vo)</span></span>;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//查询用户总条数</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Integer <span class="title">selectUserCount</span><span class="params">()</span></span>;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//查询所有用户包装类</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> List&lt;UserVo&gt; <span class="title">selectAllUserVo</span><span class="params">()</span></span>;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//多条件查询 通过用户对象中的条件查询用户列表</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> List&lt;User&gt; <span class="title">selectUserListByUser</span><span class="params">(User u)</span></span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//多条件查询 通过用户对象中的条件查询用户列表Trim</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> List&lt;User&gt; <span class="title">selectUserListByUserTrim</span><span class="params">(User u)</span></span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//更新用户表</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">updateSetUser</span><span class="params">(User u)</span></span>;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//使用多个id获取用户列表 by array</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> List&lt;User&gt; <span class="title">selectUserListByIds</span><span class="params">(Integer[] ids)</span></span>;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//使用多个id获取用户列表 by list</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> List&lt;User&gt; <span class="title">selectUserListByList</span><span class="params">(List&lt;Integer&gt; idList)</span></span>;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//使用多个id获取用户列表 by userVO</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> List&lt;User&gt; <span class="title">selectUserListByUserVo</span><span class="params">(UserVo vo)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h3 id="与数据库中相对应的实体类文件（XXX-java"   >
          <a href="#与数据库中相对应的实体类文件（XXX-java" class="heading-link"><i class="fas fa-link"></i></a><a href="#与数据库中相对应的实体类文件（XXX-java" class="headerlink" title="与数据库中相对应的实体类文件（XXX.java)"></a>与数据库中相对应的实体类文件（XXX.java)</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.sikiedu.bean;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * CREATE TABLE `user` (</span></span><br><span class="line"><span class="comment">	  `u_id` INT(11) NOT NULL AUTO_INCREMENT COMMENT &#x27;用户id&#x27;,</span></span><br><span class="line"><span class="comment">	  `u_username` VARCHAR(64) NOT NULL COMMENT &#x27;用户名&#x27;,</span></span><br><span class="line"><span class="comment">	  `u_password` VARCHAR(64) NOT NULL COMMENT &#x27;用户密码&#x27;,</span></span><br><span class="line"><span class="comment">	  `u_sex` VARCHAR(16) DEFAULT NULL COMMENT &#x27;用户性别&#x27;,</span></span><br><span class="line"><span class="comment">	  `u_createTime` DATETIME DEFAULT NULL COMMENT &#x27;用户创建时间&#x27;,</span></span><br><span class="line"><span class="comment">	  `u_cid` INT(11) NOT NULL COMMENT &#x27;用户国家id&#x27;,</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">private</span> Integer u_id;</span><br><span class="line">	<span class="keyword">private</span> String u_username;</span><br><span class="line">	<span class="keyword">private</span> String u_password;</span><br><span class="line">	<span class="keyword">private</span> String u_sex;</span><br><span class="line">	<span class="keyword">private</span> Date u_createTime;</span><br><span class="line">	<span class="keyword">private</span> Integer u_cid;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> Integer <span class="title">getU_id</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> u_id;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setU_id</span><span class="params">(Integer u_id)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.u_id = u_id;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getU_username</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> u_username;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setU_username</span><span class="params">(String u_username)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.u_username = u_username;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getU_password</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> u_password;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setU_password</span><span class="params">(String u_password)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.u_password = u_password;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getU_sex</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> u_sex;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setU_sex</span><span class="params">(String u_sex)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.u_sex = u_sex;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> Date <span class="title">getU_createTime</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> u_createTime;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setU_createTime</span><span class="params">(Date u_createTime)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.u_createTime = u_createTime;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> Integer <span class="title">getU_cid</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> u_cid;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setU_cid</span><span class="params">(Integer u_cid)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.u_cid = u_cid;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">&quot;User [u_id=&quot;</span> + u_id + <span class="string">&quot;, u_username=&quot;</span> + u_username + <span class="string">&quot;, u_password=&quot;</span> + u_password + <span class="string">&quot;, u_sex=&quot;</span> + u_sex</span><br><span class="line">				+ <span class="string">&quot;, u_createTime=&quot;</span> + u_createTime + <span class="string">&quot;, u_cid=&quot;</span> + u_cid + <span class="string">&quot;]&quot;</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>

<ol>
<li><strong>推荐类名的私有变量的名字完全与数据库中字段名相同</strong></li>
<li>给相应<strong>Set、Get方法</strong>即可</li>
<li><strong>toString方法</strong></li>
</ol>

        <h3 id="函数如何调用完整的Mybatis过程-增删改查"   >
          <a href="#函数如何调用完整的Mybatis过程-增删改查" class="heading-link"><i class="fas fa-link"></i></a><a href="#函数如何调用完整的Mybatis过程-增删改查" class="headerlink" title="函数如何调用完整的Mybatis过程(增删改查)"></a>函数如何调用完整的Mybatis过程(增删改查)</h3>
      
        <h4 id="一、selectOne-函数（查询）"   >
          <a href="#一、selectOne-函数（查询）" class="heading-link"><i class="fas fa-link"></i></a><a href="#一、selectOne-函数（查询）" class="headerlink" title="一、selectOne 函数（查询）"></a>一、selectOne 函数（查询）</h4>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line">	<span class="comment">//入门程序 通过ID 查询用户</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Test1</span><span class="params">()</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">		String resource = <span class="string">&quot;sqlMapConfig.xml&quot;</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//读取配置文件</span></span><br><span class="line">		InputStream in = Resources.getResourceAsStream(resource);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//需要sqlSessionFactoryBulider</span></span><br><span class="line">		SqlSessionFactoryBuilder ssfb = <span class="keyword">new</span> SqlSessionFactoryBuilder();</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//创建sqlSessionfactory</span></span><br><span class="line">		SqlSessionFactory ssf = ssfb.build(in);</span><br><span class="line">		 </span><br><span class="line">		<span class="comment">//生产一个sqlSession</span></span><br><span class="line">		SqlSession session = ssf.openSession();</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//操作数据库</span></span><br><span class="line">		<span class="comment">//参数1：要操作的接口方法   参数2 sql语句的参数</span></span><br><span class="line">		User user = session.selectOne(<span class="string">&quot;UserMapper.selectUserById&quot;</span>, <span class="number">1</span>);</span><br><span class="line">		System.out.println(user);</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></div></figure>


        <h4 id="二、session的getMapper方法-找到Dao层接口类（查询）"   >
          <a href="#二、session的getMapper方法-找到Dao层接口类（查询）" class="heading-link"><i class="fas fa-link"></i></a><a href="#二、session的getMapper方法-找到Dao层接口类（查询）" class="headerlink" title="二、session的getMapper方法 找到Dao层接口类（查询）"></a>二、session的getMapper方法 找到Dao层接口类（查询）</h4>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">String resource = <span class="string">&quot;sqlMapConfig.xml&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//读取配置文件</span></span><br><span class="line">InputStream in = Resources.getResourceAsStream(resource);</span><br><span class="line"><span class="comment">//需要sqlSessionFactoryBulider</span></span><br><span class="line">SqlSessionFactoryBuilder ssfb = <span class="keyword">new</span> SqlSessionFactoryBuilder();</span><br><span class="line"><span class="comment">//创建sqlSessionfactory</span></span><br><span class="line">SqlSessionFactory ssf = ssfb.build(in);</span><br><span class="line"></span><br><span class="line"><span class="comment">//将IUser加入工厂中 以getMapper找到</span></span><br><span class="line">sqlSessionFactory.getConfiguration().addMapper(IUser.class);</span><br><span class="line">SqlSession session = sqlSessionFactory.openSession();</span><br><span class="line"></span><br><span class="line">      <span class="keyword">try</span> &#123;</span><br><span class="line">	<span class="comment">//通过getMapper方法 找到IUser接口类</span></span><br><span class="line">          IUser iuser = session.getMapper(IUser.class);</span><br><span class="line">	<span class="comment">//调用相应方法（增删改查）</span></span><br><span class="line">          User user = iuser.getUserByID(<span class="number">1</span>);</span><br><span class="line">          System.out.println(<span class="string">&quot;名字：&quot;</span>+user.getName());</span><br><span class="line">      &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">          session.close();</span><br><span class="line">      &#125;</span><br></pre></td></tr></table></div></figure>

<p><strong>Mybatis的调用过程中：</strong></p>
<ol>
<li>首先需要输入流 读取配置文件sqlMapConfig.xml</li>
<li>需要一个工厂建造者Builder 来利用这个输入流建造</li>
<li>建造返回一个工厂 用这个工厂打开一次会话</li>
<li>在会话中Select…等等</li>
<li>对于更改操作（插入、更新、删除）记得<strong>session.commit()</strong></li>
<li>session的对应方法 <strong>第一个参数是通过XXXMapper.对应方法 第二个是传入的实参</strong></li>
</ol>

        <h4 id="三、session的getMapper方法-找到Dao层接口类（插入）"   >
          <a href="#三、session的getMapper方法-找到Dao层接口类（插入）" class="heading-link"><i class="fas fa-link"></i></a><a href="#三、session的getMapper方法-找到Dao层接口类（插入）" class="headerlink" title="三、session的getMapper方法 找到Dao层接口类（插入）"></a>三、session的getMapper方法 找到Dao层接口类（插入）</h4>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">....忽略配置文件加载、SQLSession建造者、工厂....</span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">	<span class="comment">//通过getMapper方法 找到IUser接口类</span></span><br><span class="line">    IUser iuser = session.getMapper(IUser.class);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//假设该接口类有 void方法insertUser(User user)</span></span><br><span class="line">    User user = <span class="keyword">new</span> User();</span><br><span class="line">	user.setXXX(<span class="string">&quot;xxx&quot;</span>);</span><br><span class="line">	...set方法</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//插入数据 记得提交会话（包括删除操作、更新操作）</span></span><br><span class="line">	iuser.insertUser(user);</span><br><span class="line">	session.commit();</span><br><span class="line">	</span><br><span class="line">&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">    session.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h4 id="四、session的getMapper方法-找到Dao层接口类（更新）"   >
          <a href="#四、session的getMapper方法-找到Dao层接口类（更新）" class="heading-link"><i class="fas fa-link"></i></a><a href="#四、session的getMapper方法-找到Dao层接口类（更新）" class="headerlink" title="四、session的getMapper方法 找到Dao层接口类（更新）"></a>四、session的getMapper方法 找到Dao层接口类（更新）</h4>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">....忽略配置文件加载、SQLSession建造者、工厂....</span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">	<span class="comment">//通过getMapper方法 找到IUser接口类</span></span><br><span class="line">    IUser iuser = session.getMapper(IUser.class);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//首先通过相应属性找到user</span></span><br><span class="line">	User user = iuser.getUserById(<span class="number">1</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//假设该接口类有 void方法updateUser(User user)</span></span><br><span class="line">	<span class="comment">//将user的name更改为Vincent</span></span><br><span class="line">	user.setName(<span class="string">&quot;Vincent&quot;</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//更新数据 记得提交会话</span></span><br><span class="line">	iuser.updateUser(user);</span><br><span class="line">	session.commit();</span><br><span class="line">	</span><br><span class="line">&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">    session.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h4 id="五、session的getMapper方法-找到Dao层接口类（删除）"   >
          <a href="#五、session的getMapper方法-找到Dao层接口类（删除）" class="heading-link"><i class="fas fa-link"></i></a><a href="#五、session的getMapper方法-找到Dao层接口类（删除）" class="headerlink" title="五、session的getMapper方法 找到Dao层接口类（删除）"></a>五、session的getMapper方法 找到Dao层接口类（删除）</h4>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">....忽略配置文件加载、SQLSession建造者、工厂....</span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">	<span class="comment">//通过getMapper方法 找到IUser接口类</span></span><br><span class="line">    IUser iuser = session.getMapper(IUser.class);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//首先通过相应属性找到user</span></span><br><span class="line">	User user = iuser.getUserById(<span class="number">1</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//假设该接口类有方法 deleteUser(User user)</span></span><br><span class="line">	<span class="comment">//记得提交会话</span></span><br><span class="line">	</span><br><span class="line">	iuser.deleteUser(user);</span><br><span class="line">	session.commit();</span><br><span class="line">	</span><br><span class="line">&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">    session.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>



        <h3 id="ResultMap：解决数据库字段与Java实体类的属性不一致问题"   >
          <a href="#ResultMap：解决数据库字段与Java实体类的属性不一致问题" class="heading-link"><i class="fas fa-link"></i></a><a href="#ResultMap：解决数据库字段与Java实体类的属性不一致问题" class="headerlink" title="ResultMap：解决数据库字段与Java实体类的属性不一致问题"></a>ResultMap：解决数据库字段与Java实体类的属性不一致问题</h3>
      
        <h4 id="1-Result实现JavaBean对象与数据库column名字不一样的映射"   >
          <a href="#1-Result实现JavaBean对象与数据库column名字不一样的映射" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-Result实现JavaBean对象与数据库column名字不一样的映射" class="headerlink" title="1.Result实现JavaBean对象与数据库column名字不一样的映射"></a>1.Result实现JavaBean对象与数据库column名字不一样的映射</h4>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- type表示具体的Java类 id随便取-唯一标识即可 --&gt;</span><br><span class="line">&lt;resultMap type=<span class="string">&quot;CountryVo&quot;</span> id=<span class="string">&quot;countryVo&quot;</span>&gt;</span><br><span class="line">		</span><br><span class="line">		&lt;!-- id标签是主键 property是Java类中的属性 column是对应Java类在数据库表中的字段 --&gt;</span><br><span class="line">		&lt;id property=<span class="string">&quot;id&quot;</span> column=<span class="string">&quot;c_id&quot;</span>/&gt;</span><br><span class="line"></span><br><span class="line">		&lt;!-- result可以是若干个其他 非主键属性的对应 --&gt;</span><br><span class="line">		&lt;result property=<span class="string">&quot;c_countryname&quot;</span> column=<span class="string">&quot;c_countryname&quot;</span>/&gt;</span><br><span class="line">		&lt;result property=<span class="string">&quot;c_capital&quot;</span> column=<span class="string">&quot;c_capital&quot;</span>/&gt;</span><br><span class="line">&lt;/resultMap&gt;</span><br></pre></td></tr></table></div></figure>


        <h4 id="2-Result实现一对多映射-Collection"   >
          <a href="#2-Result实现一对多映射-Collection" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-Result实现一对多映射-Collection" class="headerlink" title="2.Result实现一对多映射 Collection"></a>2.Result实现一对多映射 Collection</h4>
      <p>比如一个用户User 有多个帖子Post<br>现在查询对应的User 查看这个User下的所有帖子（因此帖子是集合<code>java private List&lt;Post&gt; posts;</code>)</p>
<p>1.User类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">//要实现序列化Serializable</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="keyword">implements</span> <span class="title">Serializable</span></span>&#123;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">int</span> id;</span><br><span class="line">	<span class="keyword">private</span> String username;</span><br><span class="line">	<span class="keyword">private</span> String mobile;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//一个User 对应多个Post 因此用集合</span></span><br><span class="line">	<span class="keyword">private</span> List&lt;Post&gt; posts;</span><br><span class="line">	</span><br><span class="line">	...忽略set get方法以及toString方法</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>2.Post类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Post</span> <span class="keyword">implements</span> <span class="title">Serializable</span></span>&#123;</span><br><span class="line">	<span class="comment">//查询的时候 post.id 应该等于 user.id</span></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">int</span> id;</span><br><span class="line">	<span class="keyword">private</span> User user;</span><br><span class="line">	<span class="keyword">private</span> String title;</span><br><span class="line">	<span class="keyword">private</span> String content;</span><br><span class="line">	...忽略set get方法以及toString方法</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>3.ResultMap配置一个用户多个帖子的映射</p>
<figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 在一个User中配置 多个collection --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">type</span>=<span class="string">&quot;User&quot;</span> <span class="attr">id</span>=<span class="string">&quot;resultUserMap&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;id&quot;</span> <span class="attr">column</span>=<span class="string">&quot;user_id&quot;</span> /&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;username&quot;</span> <span class="attr">column</span>=<span class="string">&quot;username&quot;</span> /&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;mobile&quot;</span> <span class="attr">column</span>=<span class="string">&quot;mobile&quot;</span> /&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- collection代表多  --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- posts代表User类中 集合的名字！column代表post表中的那个字段 与user联系 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">collection</span> <span class="attr">property</span>=<span class="string">&quot;posts&quot;</span> <span class="attr">ofType</span>=<span class="string">&quot;com.yiibai.pojo.Post&quot;</span> <span class="attr">column</span>=<span class="string">&quot;userid&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">&quot;id&quot;</span> <span class="attr">column</span>=<span class="string">&quot;post_id&quot;</span> <span class="attr">javaType</span>=<span class="string">&quot;int&quot;</span> <span class="attr">jdbcType</span>=<span class="string">&quot;INTEGER&quot;</span>/&gt;</span>    </span><br><span class="line">           <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;title&quot;</span> <span class="attr">column</span>=<span class="string">&quot;title&quot;</span> <span class="attr">javaType</span>=<span class="string">&quot;string&quot;</span> <span class="attr">jdbcType</span>=<span class="string">&quot;VARCHAR&quot;</span>/&gt;</span></span><br><span class="line">           <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;content&quot;</span> <span class="attr">column</span>=<span class="string">&quot;content&quot;</span> <span class="attr">javaType</span>=<span class="string">&quot;string&quot;</span> <span class="attr">jdbcType</span>=<span class="string">&quot;VARCHAR&quot;</span>/&gt;</span> </span><br><span class="line">	<span class="tag">&lt;/<span class="name">collection</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br></pre></td></tr></table></div></figure>

<p>4.SQL语句</p>
<figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 指定结果映射为resultUserMap 需要跟相应resultMap标签的id相同 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;getUser&quot;</span> <span class="attr">resultMap</span>=<span class="string">&quot;resultUserMap&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;int&quot;</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- 建立映射就是为了 能够映射SELECT后面的内容:u.*, p.* --&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- 没有建立映射的property返回的值不会显示在后台里 --&gt;</span></span><br><span class="line">		SELECT u.*,p.*</span><br><span class="line">		FROM user u, post p</span><br><span class="line">		WHERE u.id=p.userid AND id=#&#123;user_id&#125; </span><br><span class="line"> <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></div></figure>

<p><strong>2021.4.6更新：</strong></p>
<ol>
<li>注意都实现<strong>Serializable</strong>接口</li>
<li>注意，如果两张表中的字段重复了，记得在Select字段中起别名</li>
<li>collection中的id标签 不会指两者映射的id <strong>而只是指这个表的主键！</strong></li>
<li>真正映射一对多的 是collection中的<strong>column属性</strong></li>
</ol>
<p>起别名代码：</p>
<figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="keyword">select</span><span class="operator">&gt;</span></span><br><span class="line">	<span class="keyword">select</span> u.id <span class="keyword">as</span> u_id, u.<span class="operator">*</span>, p.id <span class="keyword">as</span> p_id,p.<span class="operator">*</span></span><br><span class="line">	<span class="keyword">from</span> <span class="keyword">user</span> u, post p</span><br><span class="line">	<span class="keyword">where</span> u.u_id <span class="operator">=</span> p.p_id <span class="keyword">AND</span> u.u_id <span class="operator">=</span> #&#123;id&#125;</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">/</span><span class="keyword">select</span><span class="operator">&gt;</span>	</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h3 id="Mybatis-plust新特性：代码生成器"   >
          <a href="#Mybatis-plust新特性：代码生成器" class="heading-link"><i class="fas fa-link"></i></a><a href="#Mybatis-plust新特性：代码生成器" class="headerlink" title="Mybatis_plust新特性：代码生成器"></a>Mybatis_plust新特性：代码生成器</h3>
      <p><strong>注意，这里的代码指的是：</strong></p>
<p><strong>1. DAO层的XXXMapper.java接口<br>2. DAO层的XXXMapper.xml文件<br>3. Model层的XXXExample.java类 方便提前处理数据</strong></p>

        <h4 id="1-需要的插件（IDEA右侧-MAVEN里面的插件双击启动）"   >
          <a href="#1-需要的插件（IDEA右侧-MAVEN里面的插件双击启动）" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-需要的插件（IDEA右侧-MAVEN里面的插件双击启动）" class="headerlink" title="1.需要的插件（IDEA右侧 MAVEN里面的插件双击启动）"></a>1.需要的插件（IDEA右侧 MAVEN里面的插件双击启动）</h4>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;plugin&gt;</span><br><span class="line">	&lt;groupId&gt;org.mybatis.generator&lt;/groupId&gt;</span><br><span class="line">	&lt;artifactId&gt;mybatis-generator-maven-plugin&lt;/artifactId&gt;</span><br><span class="line">	&lt;configuration&gt;</span><br><span class="line">		&lt;configurationFile&gt;$&#123;basedir&#125;/src/main/resources/mybatis-generator/generatorConfig.xml</span><br><span class="line">		&lt;/configurationFile&gt;</span><br><span class="line">		&lt;overwrite&gt;<span class="keyword">true</span>&lt;/overwrite&gt;</span><br><span class="line">		&lt;verbose&gt;<span class="keyword">true</span>&lt;/verbose&gt;</span><br><span class="line">	&lt;/configuration&gt;</span><br><span class="line">&lt;/plugin&gt;</span><br></pre></td></tr></table></div></figure>

<p>项目结构图：</p>
<p><img   src="https://img-blog.csdnimg.cn/20210410185916946.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>

        <h4 id="2-配置文件-generatorConfig-xml"   >
          <a href="#2-配置文件-generatorConfig-xml" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-配置文件-generatorConfig-xml" class="headerlink" title="2.配置文件 generatorConfig.xml"></a>2.配置文件 generatorConfig.xml</h4>
      <p><strong>这一步很关键，要避免多次生成重复的BaseResultMap</strong></p>
<p><strong>注意对应Mapper.java/ Mapper.xml在项目结构图中的位置</strong></p>
<figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">generatorConfiguration</span></span></span><br><span class="line"><span class="meta">        <span class="meta-keyword">PUBLIC</span> <span class="meta-string">&quot;-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN&quot;</span></span></span><br><span class="line"><span class="meta">        <span class="meta-string">&quot;http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">generatorConfiguration</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--    连接数据库jar包的路径--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">classPathEntry</span> <span class="attr">location</span>=<span class="string">&quot;C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\8.0.23/mysql-connector-java-8.0.23.jar&quot;</span>/&gt;</span></span><br><span class="line">    </span><br><span class="line">	<span class="tag">&lt;<span class="name">context</span> <span class="attr">id</span>=<span class="string">&quot;DB2Tables&quot;</span>  <span class="attr">targetRuntime</span>=<span class="string">&quot;MyBatis3&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">commentGenerator</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;suppressDate&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span>/&gt;</span></span><br><span class="line">            <span class="comment">&lt;!-- 是否去除自动生成的注释 true：是 ： false:否 --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;suppressAllComments&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">commentGenerator</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!--数据库连接参数 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">jdbcConnection</span></span></span><br><span class="line"><span class="tag">                <span class="attr">driverClass</span>=<span class="string">&quot;com.mysql.jdbc.Driver&quot;</span></span></span><br><span class="line"><span class="tag">                <span class="attr">connectionURL</span>=<span class="string">&quot;jdbc:mysql://localhost:3306/vue_demo?serverTimezone=UTC<span class="symbol">&amp;amp;</span>useSSL=true&quot;</span></span></span><br><span class="line"><span class="tag">                <span class="attr">userId</span>=<span class="string">&quot;root&quot;</span></span></span><br><span class="line"><span class="tag">                <span class="attr">password</span>=<span class="string">&quot;123456&quot;</span>&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- 避免生成重复的BaseResultMap --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;nullCatalogMeansCurrent&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">jdbcConnection</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">javaTypeResolver</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;forceBigDecimals&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">javaTypeResolver</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- 实体类的包名和存放路径 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">javaModelGenerator</span> <span class="attr">targetPackage</span>=<span class="string">&quot;com.example.demo.entity&quot;</span> <span class="attr">targetProject</span>=<span class="string">&quot;src/main/java&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;enableSubPackages&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;trimStrings&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">javaModelGenerator</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- 生成映射文件*.xml的位置--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">sqlMapGenerator</span> <span class="attr">targetPackage</span>=<span class="string">&quot;mybatis&quot;</span> <span class="attr">targetProject</span>=<span class="string">&quot;src/main/resources&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;enableSubPackages&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">sqlMapGenerator</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- 生成DAO的包名和位置 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">javaClientGenerator</span> <span class="attr">type</span>=<span class="string">&quot;XMLMAPPER&quot;</span> <span class="attr">targetPackage</span>=<span class="string">&quot;com.example.demo.repository&quot;</span> <span class="attr">targetProject</span>=<span class="string">&quot;src/main/java&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;enableSubPackages&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">javaClientGenerator</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- tableName：数据库中的表名或视图名；domainObjectName：生成的实体类的类名--&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--&lt;table tableName=&quot;book&quot; domainObjectName=&quot;Book&quot;/&gt;--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">table</span> <span class="attr">schema</span>=<span class="string">&quot;catalog&quot;</span> <span class="attr">tableName</span>=<span class="string">&quot;user&quot;</span> <span class="attr">domainObjectName</span>=<span class="string">&quot;User&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;/<span class="name">context</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">generatorConfiguration</span>&gt;</span></span><br></pre></td></tr></table></div></figure>


        <h3 id="Mybatis中出现的问题："   >
          <a href="#Mybatis中出现的问题：" class="heading-link"><i class="fas fa-link"></i></a><a href="#Mybatis中出现的问题：" class="headerlink" title="Mybatis中出现的问题："></a>Mybatis中出现的问题：</h3>
      
        <h4 id="1-UserMapper-xml到底应该放在那里？"   >
          <a href="#1-UserMapper-xml到底应该放在那里？" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-UserMapper-xml到底应该放在那里？" class="headerlink" title="1.UserMapper.xml到底应该放在那里？"></a>1.UserMapper.xml到底应该放在那里？</h4>
      <p><strong>注意，在集成SpringBoot时</strong></p>
<ol>
<li><p>首先确保 <strong>UserMapper.java要有@Mapper注解</strong></p>
</li>
<li><p>其次，要注意<strong>在SpringBoot配置文件application.yml中：</strong></p>
</li>
</ol>
<figure class="highlight yml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">mybatis:</span></span><br><span class="line">	<span class="attr">mapper-locations:</span> <span class="string">classpath:mybatis/*.xml</span></span><br></pre></td></tr></table></div></figure>

<p>并且项目结构图如下：<br><img   src="https://img-blog.csdnimg.cn/20210406191232940.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>
<ol start="3">
<li>UserMapper.xml中的namespace是UserMapper.java的具体位置</li>
</ol>
<p><strong>或者代码generator生成的配置文件中配置</strong></p>
<figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">&quot;com.example.demo.repository.UserMapper&quot;</span>&gt;</span> </span><br><span class="line"><span class="tag">&lt;/<span class="name">mapper</span>&gt;</span></span><br></pre></td></tr></table></div></figure>


        <h4 id="配置实体类应该注意的问题"   >
          <a href="#配置实体类应该注意的问题" class="heading-link"><i class="fas fa-link"></i></a><a href="#配置实体类应该注意的问题" class="headerlink" title="配置实体类应该注意的问题"></a>配置实体类应该注意的问题</h4>
      <p><strong>1. 尽量让实体类中的各个private属性名字与数据库的字段名字相同</strong></p>
<p><strong>2. 用封装类！不要用int，而是应该用<code>Integer</code></strong></p>
<p><strong>3. 如果实体类中的属性名与数据库中的字段名相同，这样mapper中参数为实体类时，才能返回</strong></p>
</div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/blog/2021/03/11/%E4%B8%8D%E4%BC%9A%E5%90%A7%E8%80%83%E7%A0%94%E8%8B%B1%E8%AF%AD/">不会吧？都2021年了，还有人觉得考研英语简单！？</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-11</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-04-09</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">5.2k</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">29分</span></span></div></header><div class="post-body"><div class="post-excerpt"><!-- 页面必备样式 -->
<style>
.back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;}
.back-top>a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height: 50px;border-radius: 50px;background-color:rgba(255,255,255,0.5);}
</style>
<div class="back-top">
    <a href="#top"> >返回顶部< </a>
</div>
<!-- toc -->
<!-- 必备样式结束 -->


        <h1 id="刚过线低分学长考研英语做题方法分享（全是干货）你还在等什么？等我吗？："   >
          <a href="#刚过线低分学长考研英语做题方法分享（全是干货）你还在等什么？等我吗？：" class="heading-link"><i class="fas fa-link"></i></a><a href="#刚过线低分学长考研英语做题方法分享（全是干货）你还在等什么？等我吗？：" class="headerlink" title="刚过线低分学长考研英语做题方法分享（全是干货）你还在等什么？等我吗？："></a>刚过线低分学长考研英语做题方法分享（全是干货）你还在等什么？等我吗？：</h1>
      
        <h1 id="作者前言"   >
          <a href="#作者前言" class="heading-link"><i class="fas fa-link"></i></a><a href="#作者前言" class="headerlink" title="作者前言"></a>作者前言</h1>
      <p>&nbsp;&nbsp;&nbsp;&nbsp;<br>考研英语分为英语一与英语二，其中英语一多为学术型研究生而准备，而英语二多为专业型研究生而准备。本文旨在总结考研一路走来<strong>自己对英语二学科的重要总结以及经验之谈</strong>，<mark>比较适合在经过一段真题复习之后抓重点、查漏补缺！</mark>希望阅读到本文的你能够有所得。Respect！</p>

        <h1 id="一、完形填空-Use-of-English"   >
          <a href="#一、完形填空-Use-of-English" class="heading-link"><i class="fas fa-link"></i></a><a href="#一、完形填空-Use-of-English" class="headerlink" title="一、完形填空 Use of English"></a>一、完形填空 Use of English</h1>
      <br>
<p>&nbsp;&nbsp;&nbsp;&nbsp;完形填空，一直都是大家不重视的科目，毕竟所占的分数不多，才 “十分”，但是非常重要！（请大家务必重视完形的做题方法）万事开头难，完形也不例外。</p>

<blockquote>
<p>Without further ado 废话不多说，我们直接进入正题~</p>
</blockquote>
<p>&nbsp;&nbsp;&nbsp;&nbsp;以下笔记以及方法论，自己总结归纳众多名师（其实没怎么看他们本人的课，都在各个网络渠道微博，知乎，哔哩哔哩等看到的方法哈哈哈）</p>
<p>完形填空我建议同学去读读 <strong>刘晓燕老师的《真题就这么点儿事》</strong> 与<strong>有道的赵楠楠老师</strong>的相关教学，正文开始！<br><br></p>

        <h2 id="1-读懂前两句-段，即读懂文章的-中心主旨句-！"   >
          <a href="#1-读懂前两句-段，即读懂文章的-中心主旨句-！" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-读懂前两句-段，即读懂文章的-中心主旨句-！" class="headerlink" title="1.读懂前两句/段，即读懂文章的 中心主旨句 ！"></a>1.读懂前两句/段，即读懂文章的 <em>中心主旨句</em> ！</h2>
      <br>
读懂文章所围绕写的中心，同时联系作者自身的态度！这一步能否做到，能决定你是否读懂文章大致走向（**同时也是阅读理解做题步骤中，关键的一步！后话。**）

<p>例如：（2015年英语二文章）<br>==In our contemporary culture== , the prospect of communicating with-or even looking at- a stranger is virtually unberable. ==Everyone== around us   seems to  ==agree==  by the way they cling to their phones, even without a __ on a subway.</p>
<p>第一句指出，<strong>当代文化（=大众 everyone）认为，与陌生人交谈，甚至看向陌生人都是不可忍受的，</strong> // 而<strong>作者</strong>的观点， <strong>大多“<em>反对</em>”大众文化。</strong>  </p>
<p>我们看看第二段怎莫硕呢：</p>
<p>  ==It’s a sad reality== —our desire to avoid interacting with other human beings—because there’s___to be gained from talking to the stranger standing by you. </p>
<p>  “sad”表明作者对此观点表示批判（小技巧：附有<u>鲜明态度</u>的形容词，一般很好地体现出作者的态度），同时==表明自己的观点（与大众相反即可）：看向陌生人，与陌生人交谈其实没那么难以忍受。==</p>
<p>在知道作者的观点与态度或者文章的主旨句之后，再去斟酌每个选项，也许可以从更宏观的方面去把握更正确更优的答案。</p>
<p>我们再来看个例子：（2013年英语二文章）</p>
<p>Given the advantages of electronic money,==you might think== that we would move quickly to the cashless society in which all payments are made electronically. ___, a ==true== cashless society ==is probably not around the corner==.</p>
<p>如出一辙，<strong>you might think = 大众的观点</strong>，即认为很快会进入“无纸化社会”，而后提到<mark>真正的无纸化社会也许还没有到来，明显提出自己的观点。</mark>因此，全文的选项，选择时都应该按照 <strong>无纸化不会到来</strong> 这个大方向来做。</p>
<br>


        <h2 id="2-具体填空处前后“词语”的逻辑关系"   >
          <a href="#2-具体填空处前后“词语”的逻辑关系" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-具体填空处前后“词语”的逻辑关系" class="headerlink" title="2.具体填空处前后“词语”的逻辑关系"></a>2.具体填空处前后“词语”的逻辑关系</h2>
      <br>
例如：（2018年英语二文章）

<p>The drive to discover is deeply rooted in humans, much the same as the basic drives <strong>for __ or shelter.</strong></p>
<p>该文章主要说明：为什么人们总是在网上浏览很负能量的评论，做一些明显很令人痛苦的事情。<mark>因为人们习惯去解决“不确定”，可能明知到事情的答案，还是想满足自己内心的好奇心。</mark></p>
<p>而聚焦到问题，说<strong>人类探索未知的渴求，如同探求__或避难所（庇护）一样</strong>，或者所连接的前后意思相近</p>
<p><mark>A.Food （食物）</mark>  B.pay（工资） C.Marriage（婚姻） D.schooling（学校教育）<br>根据排除法和常识，我们可以确定答案选 A，与 shelter 避难所含义相近。</p>
<p>譬如active 与 alive 也是具有相同意义的词语。</p>
<p>若文章的中心是反对战争支持和平，那么若某一细节处出现 *** ___war *** 选项中优先考虑 against等表示反对的介词.<br><br></p>

        <h2 id="3-具体填空处前后“语句”的逻辑关系"   >
          <a href="#3-具体填空处前后“语句”的逻辑关系" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-具体填空处前后“语句”的逻辑关系" class="headerlink" title="3.具体填空处前后“语句”的逻辑关系"></a>3.具体填空处前后“语句”的逻辑关系</h2>
      <br>
例如：（2013年英语二文章）

<p>Given the advantages of electronic money,==you might think that we would move quickly to the cashless society in which all payments are made electronically.== ___, ==a true cashless society is probably not around the corner==.</p>
<p>您也许会认为我们很快会进入无纸化社会，但是真正的无纸化社会也许可能不会很快来到。根据插入语（即填空处）前后的逻辑关系，属于虽然但是（让步状语从句），且转折处为作者观点，是重点！聚集选项：</p>
<p><mark>A. However (然而)</mark><br>B. Moreover (而且)<br>C. Therefore (因此)<br>D. Othervise (否则)</p>
<p>同样地，对于：<br>    1.总分结构再次强调作者观点句的：For instance, for example,<br><br></p>

        <h2 id="4-与文章背景相关-context-oriented-的单词更容易成为正确选项"   >
          <a href="#4-与文章背景相关-context-oriented-的单词更容易成为正确选项" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-与文章背景相关-context-oriented-的单词更容易成为正确选项" class="headerlink" title="4.与文章背景相关(context-oriented)的单词更容易成为正确选项"></a>4.与<mark>文章背景相关(context-oriented)</mark>的单词更容易成为正确选项</h2>
      <br>
例如：（2013年英语二文章）

<p><img src="https://img-blog.csdnimg.cn/20210111204641483.png" alt="在这里插入图片描述"><br>Dishonest persons 可能会进入电子支付系统并__某人的账户，本文所讲的是无纸化社会不会到了，<strong>也就是说作者对无纸化社会的态度是negative的，因而我们首先判断这个动词的褒贬方向：是好的方向，还是坏的方向；</strong> <mark>既然针对 “electronic payments system” 那么代表坏的方向</mark>，因而我们聚焦到选项：<br><img src="https://img-blog.csdnimg.cn/20210111205029885.png" alt="在这里插入图片描述"><br>A. 偷窃，盗取<br>B. 选择<br>C. 使受益<br>D. 返回，收益(pl) </p>
<p>了解到出题者的用心良苦后，我想答案不言而喻。<br><br></p>

        <h2 id="5-填空之处前后的固定搭配-amp-词组辨析"   >
          <a href="#5-填空之处前后的固定搭配-amp-词组辨析" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-填空之处前后的固定搭配-amp-词组辨析" class="headerlink" title="5.填空之处前后的固定搭配&amp;词组辨析"></a>5.填空之处前后的固定搭配&amp;词组辨析</h2>
      <br>
例如：2015年英语二真题!

<p><img src="https://img-blog.csdnimg.cn/20210112134801301.png" alt="在这里插入图片描述"><br>文章在此处阐述，与其与陌生人交谈，倒不如自己在乘车时独自闭口不谈，而在经历实验之后，大家没有觉得 “与陌生人交谈”让人尴尬。</p>
<p>聚焦选项：<br><img src="https://img-blog.csdnimg.cn/2021011213512487.png" alt="在这里插入图片描述"><br>go through with 经历 do away with 终结，废除 catch up with 追赶 put up with 容忍，忍受<br>这里用put up with是不恰当的，因为受实验者并没有完全谴责或者反对与陌生人交谈的行为。因为使用经历较好。</p>
<p>利用上文五大点，无论是宏观把握还是细节斟酌都能很好地选出最优答案，剩余的事情，就是从反复研读历年真题。找出选项与所在处前后的共性，不断总结归纳常考词汇。<br><br><br><br></p>

        <h1 id="二、阅读理解"   >
          <a href="#二、阅读理解" class="heading-link"><i class="fas fa-link"></i></a><a href="#二、阅读理解" class="headerlink" title="二、阅读理解"></a>二、阅读理解</h1>
      
        <h2 id="1-个人认为，对于考研英语试卷而言，卷子的核心是阅读！阅读是值得我们反复研读的。"   >
          <a href="#1-个人认为，对于考研英语试卷而言，卷子的核心是阅读！阅读是值得我们反复研读的。" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-个人认为，对于考研英语试卷而言，卷子的核心是阅读！阅读是值得我们反复研读的。" class="headerlink" title="1.个人认为，对于考研英语试卷而言，卷子的核心是阅读！阅读是值得我们反复研读的。"></a>1.个人认为，对于考研英语试卷而言，卷子的核心是阅读！阅读是值得我们反复研读的。</h2>
      <br>


        <h2 id="2-扎实的词汇基础，严谨的行文思路，最后辅佐于相对正确的小技巧。一定可以拿下高分！"   >
          <a href="#2-扎实的词汇基础，严谨的行文思路，最后辅佐于相对正确的小技巧。一定可以拿下高分！" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-扎实的词汇基础，严谨的行文思路，最后辅佐于相对正确的小技巧。一定可以拿下高分！" class="headerlink" title="2.扎实的词汇基础，严谨的行文思路，最后辅佐于相对正确的小技巧。一定可以拿下高分！"></a>2.扎实的词汇基础，严谨的行文思路，最后辅佐于相对正确的小技巧。一定可以拿下高分！</h2>
      <br>
对于英语而言，夯实词汇基础的重要性不言而喻，单词都不认识，更不用说读文章了。
市面上有太多太多的词汇书。

<p> 基础不太好的同学，建议跟相关老师的视频课，撇开阅读而谈词汇： <strong>朱伟老师的词汇课挺不错的！何凯文老师的必考词汇也不错，有些推荐的单词是专八系列。从词汇本身的难度而言，是比较好的，附有相近词与例句。</strong> 建议基础较好的同学，可以不用听专门的词汇课，本人<strong>反复看《十天搞定考研词汇》再辅佐历年真题（真题词汇，建议自己总结！！！）</strong>，可以达到一样的效果，</p>
<p>阅读方法正文：（本段以历年真题为例子）<br><br></p>

        <h2 id="拿到文章先看选项，只看选项即可（预热）"   >
          <a href="#拿到文章先看选项，只看选项即可（预热）" class="heading-link"><i class="fas fa-link"></i></a><a href="#拿到文章先看选项，只看选项即可（预热）" class="headerlink" title="拿到文章先看选项，只看选项即可（预热）"></a>拿到文章先看选项，只看选项即可（预热）</h2>
      <br>

<p><strong>考研阅读板块，我建议跟名师：<br>1.有道唐迟老师、赵楠楠老师<br>2.前文都、现新文道教育何凯文老师</strong></p>
<p>唐迟而言，<em><strong>串联题干而避免选项干扰</strong></em> 是对锁定文章内容、排除错误方向起着重要作用的。</p>
<p>看完选项后，根据零散的记忆，<mark>是否可以推测以下大致围绕什么主题讲。</mark> 经济？社会问题？现象？文化板块….</p>
<p>直接上题：2020年英语二真题Text1</p>
<p>第一问：<br><img src="https://img-blog.csdnimg.cn/20210113141557664.png" alt="在这里插入图片描述"><br>第二问：<br><img src="https://img-blog.csdnimg.cn/20210113141620827.png" alt="在这里插入图片描述"><br>第三问：<br><img src="https://img-blog.csdnimg.cn/20210113141646664.png" alt="在这里插入图片描述"><br>第四问：<br><img src="https://img-blog.csdnimg.cn/20210113141704780.png" alt="在这里插入图片描述"><br>第五问：<br><img src="https://img-blog.csdnimg.cn/20210113141722392.png" alt="在这里插入图片描述"><br>总览所有选项，几乎不离 <mark>Rat 老鼠 and Robot 机器人（文中的机器鼠）</mark> 单词，因而我们在读完题目之后。思考：<br>本文或许是阐述，有关于老鼠的实验，从实验中，我们应该找到：</p>
<p>1.<mark>具体人物表达的观点或态度</mark></p>
<p>2.<mark>作者的态度/若无明显态度，则作者不反对的态度基本与作者态度一致</mark><br><br></p>

        <h2 id="再读完题目后，回到文章本身，精读文章首段-首两段"   >
          <a href="#再读完题目后，回到文章本身，精读文章首段-首两段" class="heading-link"><i class="fas fa-link"></i></a><a href="#再读完题目后，回到文章本身，精读文章首段-首两段" class="headerlink" title="再读完题目后，回到文章本身，精读文章首段/首两段"></a>再读完题目后，回到文章本身，精读文章首段/首两段</h2>
      <br>

<p>熟悉吗？没错，就是完形填空的第一步！精读文章第一段，再对文章大致主题的主观推测基础上，用作者自己的话再次佐证文章的中心话题。譬如本文第一段：<br><img src="https://img-blog.csdnimg.cn/20210113161234364.png" alt="在这里插入图片描述"><br>第一句中，挑出比较重要的部分，to attuned to 意为 “ 适应，调整 或 对…敏感”。<strong>因此，开篇提到老鼠对社会信号高度敏感，以此来认清敌友。</strong> 因此纵观全文，作者应探讨老鼠与 “社会信号” 的关系。</p>
<p>再比如：2018年英语二Text2<br><img src="https://img-blog.csdnimg.cn/20210113162005224.png" alt="在这里插入图片描述"><br>这种以 “while” 引导的让步状语从句，是考研长句中比较普遍的句式。<strong>while</strong> 此时作 “尽管，虽然” 的意思，<mark>从句不重要！重要的是主句</mark>，即逗号后面的内容（<strong>未来属于例如风能、太阳能等新能源</strong>）这一句同时也是文章围绕讲的内容。因此作者既然说出来了未来是属于<strong>新能源</strong>的，那么作者对新能源应持<strong>正面，支持</strong>的态度！</p>
<br>


        <h2 id="细节题目怎么做？难，难在什么地方？"   >
          <a href="#细节题目怎么做？难，难在什么地方？" class="heading-link"><i class="fas fa-link"></i></a><a href="#细节题目怎么做？难，难在什么地方？" class="headerlink" title="细节题目怎么做？难，难在什么地方？"></a>细节题目怎么做？难，难在什么地方？</h2>
      <br>
个人认为，只需要将文章分为：

<p><strong>1.主旨题， 2.细节题， 3.例证题  4.态度题</strong></p>
<p>凡是不属于主旨题（<em>中心思想，文章题目，文章大致内容</em>）、例证题（<em>某某例子说明什么？作者通过xx想表达什么？</em>）以及态度题（<em>不管是作者还是出现的某某某</em>）的，都归于<strong>细节题，从文章某处本身上下文寻找最优答案。</strong></p>
<p>看几个例子：<br><img src="https://img-blog.csdnimg.cn/20210113162904700.png" alt="在这里插入图片描述"><br><img src="https://img-blog.csdnimg.cn/2021011316291555.png" alt="在这里插入图片描述"><br><img src="https://img-blog.csdnimg.cn/20210113162945809.png" alt="在这里插入图片描述"></p>
<p>做法因人而异，但个人觉得比较适合大家的是：<br>1.通读全文，一顿勾勾画画<br><strong>2.定位到相应的句子，读懂句子，联系中心！！！<br>3.找选项中错误的句子，找与关键句子同意替换的表达式<br>4.与文章中心靠拢！排除与中心相反或根本无关的句子</strong></p>
<p>其中，2，3，4，是较为困难的过程，同时需要大量的反复练习。揣摩到底这个选项是不是符合中心内容的，是不是对的？<strong>哪里有相应同意的表达句</strong>，或者是不是正话反说？（出现过，概率不大）</p>
<p>仍然聚焦 2020年英语二 Text1：<br><img src="https://img-blog.csdnimg.cn/20210113164018186.png" alt="在这里插入图片描述"><br><strong>Q与同事对老鼠进行测试是想看看老鼠是否会___?</strong></p>
<p>明显属于细节题，看文章内容：<br><img   src="https://img-blog.csdnimg.cn/20210113164323905.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"><br>其中，蓝色划线句是文章背景句。而红色划线句是相应重点句，文章说：为了看看这种敏感程度是否会延伸到 “非动物”身上即：（<strong>老鼠是否会对来自机器鼠的社交信号而敏感</strong>），因而与选项中比对，是否有与其同意表达的，排除与中心内容无关的，排除说法相反的。</p>
<p>选项：<br><img   src="https://img-blog.csdnimg.cn/20210113164958811.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"><br><strong>答案一定是来自文章的句子的，绝不是经过主观理解臆断出来的。</strong> 这是考研英语阅读不变应万变的宗旨。因而，A选项提到<strong>是否老鼠能够获得来自“机器鼠”的社交信号</strong> ，即是细节句的同意表达。因而正确！因为是细节句的同意表达，因而在A选项的存在情况下，其他选项不具备更优答案的气质。</p>
<p>再举个例子：<br>2020年 Text4<br><img src="https://img-blog.csdnimg.cn/20210115141124847.png" alt="在这里插入图片描述"><br> M认为，相比较于 <strong>millennials（千禧一代）</strong> ，<strong>Z世代比较___？</strong></p>
<p>根据定位出题处，<br><img   src="https://img-blog.csdnimg.cn/20210115141531762.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"><br>** 注意：题中问的<mark>Z世代</mark>如何？因此选项中可以排除千禧一代所具有的特点(flexible)。<strong>而文中提到因为零工经济 (gig economy) Z世代者寻求更多的确定性、稳定性</strong> ，有这一关键句就够了。从选项中找“确定性” “稳定性” 的同义词，同时排除相反的词。聚焦选项：</p>
<p><img src="https://img-blog.csdnimg.cn/20210115141904345.png" alt="在这里插入图片描述"><br>A 更加勤奋 （中心无关）<br>B 更加慷慨（同理）<br>C 没那么追求实际 （相反含义应排除，因为正是零工经济，所以追求实际情况）<br><strong>D 没那么冒险、比较谨慎</strong> （more certainty、stability 的同义词 = 谨慎，不冒险）</p>
<p>因此，该类型的题目的核心点：<mark> <strong>答案文中找，不找无关的，找同意替换、更贴近中心的。</strong></mark></p>
<br>


        <h2 id="例证题不找例子？那找什么？"   >
          <a href="#例证题不找例子？那找什么？" class="heading-link"><i class="fas fa-link"></i></a><a href="#例证题不找例子？那找什么？" class="headerlink" title="例证题不找例子？那找什么？"></a>例证题不找例子？那找什么？</h2>
      <br>

<p>相信听过唐迟老师课程的同学都知道， <strong>例子不重要，重要的是例子所欲支撑的观点。</strong> 没错，我们的目的就是找例子前后的观点。说来简单，做来实属不易。</p>
<p>举个例子：2019年Text2<br><img src="https://img-blog.csdnimg.cn/20210115143256658.png" alt="在这里插入图片描述"><br>作者谈到 “艰难的挑战之一” ，何意？</p>
<p>回归文章：</p>
<p><img src="https://img-blog.csdnimg.cn/20210115143520867.png" alt="在这里插入图片描述"><br>句子很长，内容比较多。但是挑出与题目更直接相关的内容。可以看到，开篇作者提出森林给了我们庇护，<strong>给了我们在应对环境变化中出现的艰难挑战</strong> ，fight against climate change 是好事情啊，为何提到是 challenge 挑战呢？原来，The climate change <strong>会使得森林排放比自身吸收得更多的碳元素</strong> = <strong>入不敷出</strong> ，<mark>因而有了这一关键句就够了。做题时，抓住好事会变成坏事，找<strong>森林给了我们艰难挑战</strong>的同意表达：</mark></p>
<p><img src="https://img-blog.csdnimg.cn/20210115144129782.png" alt="在这里插入图片描述"><br> <img src="https://img-blog.csdnimg.cn/20210115144136539.png" alt="在这里插入图片描述"><br>相比较而言，<strong>森林给了我们艰难挑战</strong>对应同意表达的选项是<br><strong>【B】forests may become a potential threat 森林有可能成为潜在的威胁 = 艰难挑战 = 排放出多于自身吸收的碳元素</strong></p>
<p>再看一个例子：2021年 Text 2</p>
<p><img src="https://img-blog.csdnimg.cn/20210115145244463.png" alt="在这里插入图片描述"><br>L大学的报告显示，在英国，____</p>
<p>回归文章：</p>
<p><img src="https://img-blog.csdnimg.cn/20210115144952172.png" alt="在这里插入图片描述"><br>划线句子是关键句，<strong>根据报道，在英国，全国85%的土地与肉产品、奶制品直接相关。</strong> 读文章时，有些必要成分是一定要揣摩的，比如 “数字” “书名号” “感叹号” <mark>作者不会无缘无故提到数字，不会无缘无故地提到某书的命名，同时也不会无缘无故地感叹、发表观点。</mark></p>
<p>比如这里，文中出现 85% ，<em>心中就要想，这个数字代表什么？想要强调什么？好事还是坏事？会不会与作者态度相关？</em></p>
<p>找到关键句后，回归选项：</p>
<p>  [A] farmland has been inefficiently utilised<br>  [B] factory style production needs reforming<br>  <strong>[C] most land is used for meat and dairy production</strong><br>  [D] more green fields will be converted for farming</p>
<p>因而经过<strong>关键句的同意表达：85% percent = most land</strong> 再三斟酌，可以选出正确答案 C选项。<br><br></p>

        <h2 id="“主旨题”-约等于-≈-观点态度题？"   >
          <a href="#“主旨题”-约等于-≈-观点态度题？" class="heading-link"><i class="fas fa-link"></i></a><a href="#“主旨题”-约等于-≈-观点态度题？" class="headerlink" title="“主旨题” 约等于 ≈ 观点态度题？"></a>“主旨题” 约等于 ≈ 观点态度题？</h2>
      <p>文章的主旨就是整个文章的心脏，文章主旨当然大部分是与作者观点态度直接相关的。因而，做文章时，应首先明确自己的做题目标：</p>
<p>①、文章主旨/文章中心 </p>
<p>②、作者观点（是否相同/相反）</p>
<p>这一块我还是力挺唐迟老师的做法：<strong>回文法。</strong><br>如果给你ABCD四个标题，或者四个文章主旨，你会怎么写文章？</p>
<p>例如：2016年text3<br><img src="https://img-blog.csdnimg.cn/20210115150846804.png" alt="在这里插入图片描述"><br>直接看第一段，思考中心话题</p>
<p><img src="https://img-blog.csdnimg.cn/20210115151027413.png" alt="在这里插入图片描述"><br>每个人都太忙了，这已经是Cliche（陈词滥调，老生常谈）了。<strong>而抱怨声尤其来自：没时间阅读。</strong> 至此，我们已经可以猜测到，这篇文章<strong>是讲没时间阅读这个现象</strong>，作者一定会围绕这个中心话题展开探讨。</p>
<p><img   src="https://img-blog.csdnimg.cn/20210115151317671.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"><br>因而，答案一目了然。 <mark>How to Find Time to Read 如何空出时间来阅读？</mark> 这不就是中心话题吗？反过来说，如果是命题作文：如何空出时间阅读？</p>
<p>第一步当然就是陈述现象！阐述随着当今世界发展，越来越多的人哀嚎：没有时间阅读。然后继续行文…</p>
<p>再举个例子：</p>
<p><img src="https://img-blog.csdnimg.cn/20210115151704858.png" alt="在这里插入图片描述"><br>问作者态度，针对英国政府目前在<strong>运动</strong>方面所作出的努力。</p>
<p>回归文章：</p>
<p><img   src="https://img-blog.csdnimg.cn/20210115152007604.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"><br>The Parkrun phenomenon 本地公园跑现象的成功，更加凸显<strong>伦敦奥运遗风的失败！</strong>，为全文点出<strong>批评英国政府的总基调</strong>。作者提到：十年前，Planning documents 承诺人们会更苗条，更健康，然后事情并不如人意，<strong>Officials 并没有激励新一代更多地参与体育！</strong> </p>
<p>聚焦选项：<br><img   src="https://img-blog.csdnimg.cn/20210115152459147.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"><br>A 忍受，容忍<br>B 批判，批评<br>C 不确定的<br>D 同情的</p>
<p>根据作者所表达的态度，<strong>伦敦奥运遗风的失败！</strong>  <strong>批评英国政府的总基调,</strong>  <strong>Officials 并没有激励新一代更多地参与体育！</strong> 我们可以确定，作者<strong>对英国政府持批评态度</strong>！<br><br></p>

        <h1 id="三、新题型（阅读理解的“儿子”：定位-关键句-比对）"   >
          <a href="#三、新题型（阅读理解的“儿子”：定位-关键句-比对）" class="heading-link"><i class="fas fa-link"></i></a><a href="#三、新题型（阅读理解的“儿子”：定位-关键句-比对）" class="headerlink" title="三、新题型（阅读理解的“儿子”：定位-关键句-比对）"></a>三、新题型（阅读理解的“儿子”：定位-关键句-比对）</h1>
      <br>

<p>为什么这么说？很简单，不管是<strong>小标题题型</strong>（每段话缺标题，给出若干个标题，一一对应）<br><img   src="https://img-blog.csdnimg.cn/20210121143412673.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"><br><img   src="https://img-blog.csdnimg.cn/20210121143418117.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>
<p>还是<strong>概括句题</strong> （文章后有一个表格，将左边的事物与右边的表述一一正确对应）</p>
<p><img   src="https://img-blog.csdnimg.cn/20210121143449877.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"><br>均是在<mark>在阅读文章，完全理解了文章之后！对阅读到的信息进行归纳总结。</mark></p>
<p><strong>新题型板块，我建议：<br>有道赵楠楠老师</strong></p>
<p>可以说对于两种题型，方法大同小异：</p>
<p><mark>1.定位<br>    2.找同义替换<br>    3.回归选项</p>
<p>带着做题思路做真题，例如2018年小标题题型，放出链接如下：</p>
<p><span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://wenku.baidu.com/view/a3ba8cab900ef12d2af90242a8956bec0975a5df.html" >2018年真题及答案（转载自百度文库）</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span></p>
<p>第一步：<strong>读选项，混个眼熟</strong><br>第二步：读<strong>文章标题</strong><br>第三步：直接聚焦相应段落：<strong>找段落重要句</strong></p>
<p>该文章的标题：<br><img src="https://img-blog.csdnimg.cn/20210121144526721.png" alt="在这里插入图片描述"><br>与人交谈的五种方法</p>
<p><img   src="https://img-blog.csdnimg.cn/20210121144832291.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"><br>红色是关键句，作者提到：<br><strong>I know the feeling and here is my advice: just get it (想说的话) out.</strong> 读完回归众多选项，进行同义替换：</p>
<p><img   src="https://img-blog.csdnimg.cn/20210121145023812.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"><br><strong>Just say it = Just get it out 同意替换，因此相比之下，A为正确答案</strong></p>
<p>再比如：</p>
<p><img src="https://img-blog.csdnimg.cn/20210121145651565.png" alt="在这里插入图片描述"><br><strong>Find the things which you and that person have in common</strong> 说白了就是找到你与交谈对象所具有的共同点！找到共同点后，两人才能建立connection，才能使得谈话更顺利！</p>
<p>聚焦选项，找与表达 “找共同点” 的同义替换：</p>
<p><img   src="https://img-blog.csdnimg.cn/20210121145855141.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"><br>再三斟酌，<strong>Find the “me too”s = have in common</strong>   能够形象地表达出共同点！因而，E成为正确答案。</p>
<br>


        <h1 id="四、翻译"   >
          <a href="#四、翻译" class="heading-link"><i class="fas fa-link"></i></a><a href="#四、翻译" class="headerlink" title="四、翻译"></a>四、翻译</h1>
      <br>

<p>翻译是可以说是，英语学科中，最难也是最重要的板块！这个板块中，我建议</p>
<p><strong>前新东方名师，现跟谁学名师-教翻译的唐静老师</strong></p>
<p>唐静老师所讲授的知识点特别他的《拆分与组合》，能够从根本上解决无法对句子进行长句断翻、短句补译、整理中英译法之差别等技巧。踏踏实实学翻译。</p>
<p>我建议：</p>
<p><strong>1.跟唐静老师学视频课<br>2.利用学到的技巧，用在历年阅读真题上，精读文章中的句子！</strong></p>

        <h1 id="五、作文"   >
          <a href="#五、作文" class="heading-link"><i class="fas fa-link"></i></a><a href="#五、作文" class="headerlink" title="五、作文"></a>五、作文</h1>
      <br>

<p>作文板块，我推荐<strong>王江涛老师的《考研英语高分写作》</strong>，基础不好的同学需要听王江涛老师的视频课，反复听！较好的有该书即可，并辅佐自己平时的总结！<strong>英语作文范文不在于多，而在于精，因此我建议范文处理方法：</strong></p>
<ol>
<li>针对<strong>历年真题的五篇不同话题/类型范文</strong></li>
<li>《高分写作》老师所给<strong>预测五篇</strong>不同话题/类型</li>
<li>《高分写作》上每个板块的<strong>作文拓展词汇</strong></li>
</ol>
<p>最后，利用已有反复诵读的范文模板，尝试去做新题，输出学到的相应必备句型！<strong>模板的作用是在于用，不在于记！因而，要多用，否则考场上也无法很快地进入状态。</strong></p>
</div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/blog/2021/03/11/C_C++%20%E7%BC%96%E7%A8%8B%E5%B8%B8%E8%A7%81%E4%BB%A3%E7%A0%81/">C/C++ 编程常见代码</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-11</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-05-06</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">7.8k</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">76分</span></span></div></header><div class="post-body"><div class="post-excerpt">
        <h2 id="1-加法器、累加器：（多实现-1-2-…-n-以及阶乘-n-的功能）"   >
          <a href="#1-加法器、累加器：（多实现-1-2-…-n-以及阶乘-n-的功能）" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-加法器、累加器：（多实现-1-2-…-n-以及阶乘-n-的功能）" class="headerlink" title="1.加法器、累加器：（多实现 1+2+…+n 以及阶乘 n! 的功能）"></a>1.加法器、累加器：（多实现 1+2+…+n 以及阶乘 n! 的功能）</h2>
      
        <h4 id="一、求累加"   >
          <a href="#一、求累加" class="heading-link"><i class="fas fa-link"></i></a><a href="#一、求累加" class="headerlink" title="一、求累加"></a>一、求累加</h4>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">int</span> i =<span class="number">0</span>;</span><br><span class="line">		<span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">		<span class="keyword">int</span> n;</span><br><span class="line">		cin &gt;&gt; n;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">while</span>(i&lt;=n)&#123;	</span><br><span class="line">			res += i;</span><br><span class="line">			i++;	</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">//其中 res为最终结果 n表示加到多少 i表示每次加的数 </span></span><br><span class="line">		</span><br><span class="line">		cout &lt;&lt; res &lt;&lt;endl;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></div></figure>

        <h4 id="二、求某数阶乘"   >
          <a href="#二、求某数阶乘" class="heading-link"><i class="fas fa-link"></i></a><a href="#二、求某数阶乘" class="headerlink" title="二、求某数阶乘"></a>二、求某数阶乘</h4>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> i = <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">int</span> sum = <span class="number">1</span>; </span><br><span class="line">	<span class="keyword">int</span> n;</span><br><span class="line">	cin &gt;&gt; n;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">while</span>(i &lt;=n)&#123;</span><br><span class="line">		</span><br><span class="line">		sum *=i;    <span class="comment">// 1. sum = 1*1 =1</span></span><br><span class="line">		i++;		<span class="comment">// 2. i =2;</span></span><br><span class="line">					<span class="comment">// 3. sum = 1*2=2 以此类推 </span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;其阶乘为：&quot;</span> &lt;&lt; sum &lt;&lt;endl;</span><br><span class="line">	</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h2 id="2-求两个数或数组中的最大值与最小值"   >
          <a href="#2-求两个数或数组中的最大值与最小值" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-求两个数或数组中的最大值与最小值" class="headerlink" title="2. 求两个数或数组中的最大值与最小值"></a>2. 求两个数或数组中的最大值与最小值</h2>
      <p>int main(){</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">	<span class="keyword">int</span> a, b,max;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;请连续输入待比较的两个数，空格隔开：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	cin &gt;&gt; a &gt;&gt; b;</span><br><span class="line">	max = (a&gt;b) ? a : b; <span class="comment">//使用双目运算符取得最大值</span></span><br><span class="line">	cout &lt;&lt; max &lt;&lt;endl;</span><br><span class="line">		</span><br><span class="line">	<span class="keyword">int</span> arr[<span class="number">5</span>] = &#123;<span class="number">5</span>, <span class="number">2</span>, <span class="number">13</span>, <span class="number">23</span>, <span class="number">66</span>&#125;;</span><br><span class="line">	<span class="keyword">int</span> arr_max, arr_min;</span><br><span class="line">	arr_max = arr[<span class="number">0</span>]; <span class="comment">//默认第一位元素为最值 </span></span><br><span class="line">	arr_min = arr[<span class="number">0</span>];</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i =<span class="number">0</span>; i &lt;<span class="number">5</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">			<span class="keyword">if</span>(arr_max &lt; arr[i])</span><br><span class="line">			&#123;</span><br><span class="line">				arr_max = arr[i];		</span><br><span class="line">			&#125;<span class="keyword">else</span> <span class="keyword">if</span>(arr_min &gt; arr[i])&#123;  <span class="comment">//往后遍历，若还有更大的则更新最值 否则最小值更新</span></span><br><span class="line">				arr_min = arr[i]; </span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	cout &lt;&lt; arr_min &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt;arr_max &lt;&lt;endl;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>

        <h2 id="3-冒泡排序，必须会默写"   >
          <a href="#3-冒泡排序，必须会默写" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-冒泡排序，必须会默写" class="headerlink" title="3.冒泡排序，必须会默写"></a>3.冒泡排序，必须会默写</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> LEN 5</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> a[LEN] = &#123;<span class="number">2</span>, <span class="number">13</span>, <span class="number">5</span>, <span class="number">55</span>, <span class="number">20</span>&#125;;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;LEN; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>; j&lt;LEN-i<span class="number">-1</span>; j++)&#123;</span><br><span class="line">		 <span class="comment">//注意两个for循环的初始条件与终止条件 都从0k开始</span></span><br><span class="line">			<span class="keyword">if</span>(a[j] &gt; a[j+<span class="number">1</span>])&#123;</span><br><span class="line">				<span class="keyword">int</span>  t = a[j];</span><br><span class="line">				a[j] = a[j+<span class="number">1</span>];</span><br><span class="line">				a[j+<span class="number">1</span>] = t;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h2 id="4-简单的switch-case多分支条件语句"   >
          <a href="#4-简单的switch-case多分支条件语句" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-简单的switch-case多分支条件语句" class="headerlink" title="4.简单的switch-case多分支条件语句"></a>4.简单的switch-case多分支条件语句</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> score;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;请在0~100范围内输入你的分数(满分100分)：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot; 60分以下不及格 60~70及格 70~80良好 80以上优秀  &quot;</span> &lt;&lt;endl;</span><br><span class="line">	cin &gt;&gt; score;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in"><span class="keyword">switch</span></span>(score/<span class="number">10</span>)&#123;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">6</span> :</span><br><span class="line">			cout&lt;&lt;<span class="string">&quot;及格&quot;</span>&lt;&lt;endl; </span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">7</span> :</span><br><span class="line">			cout&lt;&lt;<span class="string">&quot;良好&quot;</span>&lt;&lt;endl; </span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">8</span> :</span><br><span class="line">			cout&lt;&lt;<span class="string">&quot;优秀&quot;</span>&lt;&lt;endl; </span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">9</span> :</span><br><span class="line">			cout&lt;&lt;<span class="string">&quot;优秀&quot;</span>&lt;&lt;endl; </span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">10</span> :</span><br><span class="line">			cout&lt;&lt;<span class="string">&quot;满分&quot;</span>&lt;&lt;endl; </span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">default</span>:</span><br><span class="line">			cout&lt;&lt;<span class="string">&quot;不及格&quot;</span>&lt;&lt;endl; </span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>

        <h2 id="5-求两个数的最大公约数、最小公倍数-不可含0-谭浩强C语言例题）"   >
          <a href="#5-求两个数的最大公约数、最小公倍数-不可含0-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-求两个数的最大公约数、最小公倍数-不可含0-谭浩强C语言例题）" class="headerlink" title="5.求两个数的最大公约数、最小公倍数(不可含0) (谭浩强C语言例题）"></a>5.求两个数的最大公约数、最小公倍数(不可含0) (谭浩强C语言例题）</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> x, y, r;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;请输入待运算的两个数, 中间用空格隔开&quot;</span> &lt;&lt;endl; </span><br><span class="line">	cin &gt;&gt; x &gt;&gt; y;</span><br><span class="line">	<span class="keyword">int</span> mul = x * y;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span>(x &gt; y) <span class="comment">//将比如 8,2 与 2,8 的操作统一输出</span></span><br><span class="line">	&#123;</span><br><span class="line">		r = x;</span><br><span class="line">		x = y;</span><br><span class="line">		y = r;  </span><br><span class="line">	 &#125; </span><br><span class="line">	 </span><br><span class="line">	r = x % y;  <span class="comment">//直接判断两数是否可以整除 </span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">while</span>(r)&#123;</span><br><span class="line">		x = y;</span><br><span class="line">		y = r;</span><br><span class="line">		r = x % y; </span><br><span class="line">		<span class="comment">//辗转相除法</span></span><br><span class="line">		<span class="comment">//直到余数不为0 </span></span><br><span class="line">		<span class="comment">//将余数作为除数 较小者作为被除数 继续求余判断</span></span><br><span class="line">	&#125;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;最大公约数:&quot;</span> &lt;&lt; y &lt;&lt; <span class="string">&quot;，最小公倍数:&quot;</span> &lt;&lt; mul / y &lt;&lt;endl;	 </span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>

        <h2 id="6-斐波那契数列递归"   >
          <a href="#6-斐波那契数列递归" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-斐波那契数列递归" class="headerlink" title="6.斐波那契数列递归"></a>6.斐波那契数列递归</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">fib</span><span class="params">(<span class="keyword">int</span> n)</span></span>&#123;</span><br><span class="line">	<span class="keyword">if</span>(n == <span class="number">1</span>)&#123;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;<span class="keyword">else</span> <span class="keyword">if</span>(n == <span class="number">2</span>)&#123;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">return</span> <span class="built_in">fib</span>(n<span class="number">-2</span>) + <span class="built_in">fib</span>(n<span class="number">-1</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
<p>1, 1, 2, 3, 5…以此类推</p>

        <h2 id="7-判断某数是否是素数-谭浩强C语言例题）"   >
          <a href="#7-判断某数是否是素数-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-判断某数是否是素数-谭浩强C语言例题）" class="headerlink" title="7.判断某数是否是素数 (谭浩强C语言例题）"></a>7.判断某数是否是素数 (谭浩强C语言例题）</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="keyword">int</span> n, i;</span><br><span class="line">  <span class="keyword">bool</span> isPrime = <span class="literal">true</span>;</span><br><span class="line"> </span><br><span class="line">  cout &lt;&lt; <span class="string">&quot;输入一个正整数: &quot;</span>;</span><br><span class="line">  cin &gt;&gt; n;</span><br><span class="line"> </span><br><span class="line">  <span class="keyword">for</span>(i = <span class="number">2</span>; i &lt;= n / <span class="number">2</span>; ++i)</span><br><span class="line">  &#123;</span><br><span class="line">      <span class="keyword">if</span>(n % i == <span class="number">0</span>) <span class="comment">//只能被1 和 自身整除的数 才能是素数 </span></span><br><span class="line">      &#123;</span><br><span class="line">          isPrime = <span class="literal">false</span>;</span><br><span class="line">          <span class="keyword">break</span>;</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">if</span> (isPrime)</span><br><span class="line">      cout &lt;&lt; <span class="string">&quot;是素数&quot;</span>;</span><br><span class="line">  <span class="keyword">else</span></span><br><span class="line">      cout &lt;&lt; <span class="string">&quot;不是素数&quot;</span>;</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="8-大写字母转化为小写字母"   >
          <a href="#8-大写字母转化为小写字母" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-大写字母转化为小写字母" class="headerlink" title="8.大写字母转化为小写字母"></a>8.大写字母转化为小写字母</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">char</span> a;</span><br><span class="line">	a = <span class="built_in">getchar</span>();</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%c&quot;</span>, a+<span class="number">32</span>); <span class="comment">// C或C语言中字符数据均以ASCII值存储 大写字母与小写字母相差32</span></span><br><span class="line">	</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h2 id="9-统计输入字符中单词的个数-谭浩强C语言例题）"   >
          <a href="#9-统计输入字符中单词的个数-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#9-统计输入字符中单词的个数-谭浩强C语言例题）" class="headerlink" title="9.统计输入字符中单词的个数 (谭浩强C语言例题）"></a>9.统计输入字符中单词的个数 (谭浩强C语言例题）</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 30</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">char</span> b[N];</span><br><span class="line">	<span class="keyword">int</span> word,count=<span class="number">0</span>,i; <span class="comment">//word代表是否是单词 1代表是 0代表不是单词 而是空格 </span></span><br><span class="line">						<span class="comment">//count 代表单词个数	 </span></span><br><span class="line">	<span class="built_in">puts</span>(<span class="string">&quot;输入的字符长度要小于等于N&quot;</span>); <span class="comment">// 例如-&gt;  hello world </span></span><br><span class="line">	<span class="built_in">gets</span>(b);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">0</span>;b[i]!=<span class="string">&#x27;\0&#x27;</span>;i++)</span><br><span class="line">		<span class="keyword">if</span>(b[i]==<span class="string">&#x27; &#x27;</span>)  <span class="comment">//若为空格则不是单词 </span></span><br><span class="line">		   word=<span class="number">0</span>; </span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">if</span>(word==<span class="number">0</span>) <span class="comment">//若是空格则遇到的新单词使得count加一 </span></span><br><span class="line">		&#123;</span><br><span class="line">			count++;</span><br><span class="line">			word=<span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>,count);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="10-求Sn-a-aa-aaa-…-aa…aaa（有n个a）之值，其中a是一个数字，为2。-例如，n-5时-2-22-222-2222-22222，n由键盘输入。-谭浩强C语言例题）"   >
          <a href="#10-求Sn-a-aa-aaa-…-aa…aaa（有n个a）之值，其中a是一个数字，为2。-例如，n-5时-2-22-222-2222-22222，n由键盘输入。-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-求Sn-a-aa-aaa-…-aa…aaa（有n个a）之值，其中a是一个数字，为2。-例如，n-5时-2-22-222-2222-22222，n由键盘输入。-谭浩强C语言例题）" class="headerlink" title="10.求Sn=a+aa+aaa+…+aa…aaa（有n个a）之值，其中a是一个数字，为2。 例如，n=5时=2+22+222+2222+22222，n由键盘输入。 (谭浩强C语言例题）"></a>10.求Sn=a+aa+aaa+…+aa…aaa（有n个a）之值，其中a是一个数字，为2。 例如，n=5时=2+22+222+2222+22222，n由键盘输入。 (谭浩强C语言例题）</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">int</span> n,i,Sn =<span class="number">0</span>,a=<span class="number">0</span>;</span><br><span class="line">    cin &gt;&gt; n;  <span class="comment">//n代表循环次数</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>; i&lt;n; i++)&#123;</span><br><span class="line">    	a = a*<span class="number">10</span> +<span class="number">2</span>;   <span class="comment">// 0, 2, 22, 222 </span></span><br><span class="line">    	Sn += a;		/<span class="number">0</span>+<span class="number">2</span>+<span class="number">22</span> </span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; Sn;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="11-求Sn-1-2-3-4-5-…-n-之值，其中n是一个数字-n不超过20-注意使用int类型-当n为19时会溢出-谭浩强C语言例题）"   >
          <a href="#11-求Sn-1-2-3-4-5-…-n-之值，其中n是一个数字-n不超过20-注意使用int类型-当n为19时会溢出-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#11-求Sn-1-2-3-4-5-…-n-之值，其中n是一个数字-n不超过20-注意使用int类型-当n为19时会溢出-谭浩强C语言例题）" class="headerlink" title="11.求Sn=1!+2!+3!+4!+5!+…+n!之值，其中n是一个数字(n不超过20) 注意使用int类型 当n为19时会溢出 (谭浩强C语言例题）"></a>11.求Sn=1!+2!+3!+4!+5!+…+n!之值，其中n是一个数字(n不超过20) 注意使用int类型 当n为19时会溢出 (谭浩强C语言例题）</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">fac</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> n)</span></span>&#123;</span><br><span class="line">	<span class="keyword">long</span> <span class="keyword">long</span> i = <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">long</span> <span class="keyword">long</span> sum = <span class="number">1</span>; </span><br><span class="line">	<span class="keyword">while</span>(i &lt;=n)&#123;</span><br><span class="line">		sum *=i;    <span class="comment">// 1. sum = 1*1 =1</span></span><br><span class="line">		i++;		<span class="comment">// 2. i =2;</span></span><br><span class="line">					<span class="comment">// 3. sum = 1*2=2 以此类推 </span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> sum;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">long</span> <span class="keyword">long</span> n,sum=<span class="number">0</span>,a = <span class="number">0</span>;</span><br><span class="line">	cin &gt;&gt; n;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">long</span> <span class="keyword">long</span> i=n; i&gt;<span class="number">0</span>; i--)&#123;</span><br><span class="line">		sum += <span class="built_in">fac</span>(i); </span><br><span class="line">	&#125;</span><br><span class="line">	cout &lt;&lt; sum &lt;&lt; endl;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="12-求一个3×3矩阵对角线元素之和。-谭浩强C语言例题）"   >
          <a href="#12-求一个3×3矩阵对角线元素之和。-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#12-求一个3×3矩阵对角线元素之和。-谭浩强C语言例题）" class="headerlink" title="12.求一个3×3矩阵对角线元素之和。 (谭浩强C语言例题）"></a>12.求一个3×3矩阵对角线元素之和。 (谭浩强C语言例题）</h2>
      <p><img   src="https://img-blog.csdnimg.cn/20210116103426548.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">int</span> a[<span class="number">3</span>][<span class="number">3</span>];</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;<span class="number">3</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j&lt;<span class="number">3</span>; j++)&#123;</span><br><span class="line">			cin &gt;&gt; a[i][j];</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;  <span class="comment">//两个for循环输入二维数组</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">int</span> sum1 = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">int</span> sum2 = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;<span class="number">3</span>;i++)</span><br><span class="line">	&#123;</span><br><span class="line">	  sum1 += a[i][i]; <span class="comment">//主对角线求和</span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">int</span> j=<span class="number">2</span>;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;<span class="number">3</span>;i++)</span><br><span class="line">	 &#123; </span><br><span class="line">	 	</span><br><span class="line">	 	sum2 += a[i][j]; </span><br><span class="line">	 	<span class="comment">//副对角线求和 注意变量j定义在循环外 不然值不会发生改变</span></span><br><span class="line">		<span class="comment">//j分别是2 1 0 </span></span><br><span class="line">		 j--;</span><br><span class="line">	 	</span><br><span class="line">	 &#125;</span><br><span class="line">	cout &lt;&lt; sum1 &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; sum2;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>


        <h2 id="13-用选择法对10个整数从小到大排序。-谭浩强C语言例题）"   >
          <a href="#13-用选择法对10个整数从小到大排序。-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#13-用选择法对10个整数从小到大排序。-谭浩强C语言例题）" class="headerlink" title="13.用选择法对10个整数从小到大排序。 (谭浩强C语言例题）"></a>13.用选择法对10个整数从小到大排序。 (谭浩强C语言例题）</h2>
      <p> <img   src="https://img-blog.csdnimg.cn/20210116105046701.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> LEN 10</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">swap</span><span class="params">(<span class="keyword">int</span> *a,<span class="keyword">int</span> *b)</span>  <span class="comment">//交换两个数 引用&amp; 或者指针* 均可</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> temp = *a;</span><br><span class="line">    *a = *b;</span><br><span class="line">    *b = temp;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">selection_sort</span><span class="params">(<span class="keyword">int</span> arr[], <span class="keyword">int</span> len)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> i,j;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (i = <span class="number">0</span> ; i &lt; len - <span class="number">1</span> ; i++)</span><br><span class="line">    &#123;</span><br><span class="line">                <span class="keyword">int</span> min = i; <span class="comment">//默认选择最小值 </span></span><br><span class="line">                <span class="keyword">for</span> (j = i + <span class="number">1</span>; j &lt; len; j++)    </span><br><span class="line">                        <span class="keyword">if</span> (arr[j] &lt; arr[min])    </span><br><span class="line">                                min = j;    		</span><br><span class="line">                <span class="built_in">swap</span>(&amp;arr[min], &amp;arr[i]);    </span><br><span class="line">                <span class="comment">//调用数组元素时 元素前加引用 </span></span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">int</span> a[LEN];</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;LEN; i++)</span><br><span class="line">		cin &gt;&gt; a[i];</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">selection_sort</span>(a, LEN);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;LEN; i++)&#123;</span><br><span class="line">		cout &lt;&lt; a[i] &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h2 id="14-数组插入处理，对一个排好序的数组，按原来的规律插入某数"   >
          <a href="#14-数组插入处理，对一个排好序的数组，按原来的规律插入某数" class="heading-link"><i class="fas fa-link"></i></a><a href="#14-数组插入处理，对一个排好序的数组，按原来的规律插入某数" class="headerlink" title="14.数组插入处理，对一个排好序的数组，按原来的规律插入某数"></a>14.数组插入处理，对一个排好序的数组，按原来的规律插入某数</h2>
      <p><img   src="https://img-blog.csdnimg.cn/20210120142711373.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> a[<span class="number">5</span>],x;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;<span class="number">5</span>; i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span> ,&amp;a[i]); <span class="comment">//输入数组</span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>, &amp;x); <span class="comment">//输入待插入元素</span></span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;<span class="number">5</span>; i++)&#123;</span><br><span class="line">		<span class="keyword">if</span>(a[i]&lt;x &amp;&amp; a[i+<span class="number">1</span>]&gt;x)  <span class="comment">//满足条件模拟“插入” 并输入前后元素 </span></span><br><span class="line">		cout &lt;&lt; a[i] &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt;x &lt;&lt;<span class="string">&quot; &quot;</span>;</span><br><span class="line">		<span class="keyword">else</span> </span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, a[i]); <span class="comment">//否则正常输入</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>

        <h2 id="15-写一个函数，使给定的一个二维数组（３×３）转置，即行列互换。-谭浩强C语言例题）"   >
          <a href="#15-写一个函数，使给定的一个二维数组（３×３）转置，即行列互换。-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#15-写一个函数，使给定的一个二维数组（３×３）转置，即行列互换。-谭浩强C语言例题）" class="headerlink" title="15.写一个函数，使给定的一个二维数组（３×３）转置，即行列互换。 (谭浩强C语言例题）"></a>15.写一个函数，使给定的一个二维数组（３×３）转置，即行列互换。 (谭浩强C语言例题）</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 3 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> a[N][N];</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;N;i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j&lt;N;j++)</span><br><span class="line">		&#123;</span><br><span class="line">			cin &gt;&gt; a[i][j];</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;N;i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j&lt;N;j++)</span><br><span class="line">		&#123;</span><br><span class="line">			cout &lt;&lt; a[j][i] &lt;&lt;<span class="string">&quot; &quot;</span>; <span class="comment">//行列互换即可</span></span><br><span class="line">		&#125;</span><br><span class="line">		cout &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>


        <h2 id="16-自定义函数之字符串反转"   >
          <a href="#16-自定义函数之字符串反转" class="heading-link"><i class="fas fa-link"></i></a><a href="#16-自定义函数之字符串反转" class="headerlink" title="16.自定义函数之字符串反转"></a>16.自定义函数之字符串反转</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">char</span> a[N];</span><br><span class="line">	cin &gt;&gt; a; </span><br><span class="line">	<span class="comment">//遇tab、空格、回车都结束 </span></span><br><span class="line">	<span class="comment">//适合输入单个字符串且中间不含空格 </span></span><br><span class="line">	<span class="comment">//若需输入含空格的字符串 可用gets(str);</span></span><br><span class="line">	<span class="keyword">int</span> len = <span class="built_in">strlen</span>(a);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=len<span class="number">-1</span>; i&gt;=<span class="number">0</span>; i--)&#123; </span><br><span class="line">	<span class="comment">//注意字符数组的最后一个字符是 &#x27;\0&#x27;因而从倒数第二个开始</span></span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%c&quot;</span>, a[i]);</span><br><span class="line">	&#125; </span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>


        <h2 id="17-写一函数，将两个字符串连接"   >
          <a href="#17-写一函数，将两个字符串连接" class="heading-link"><i class="fas fa-link"></i></a><a href="#17-写一函数，将两个字符串连接" class="headerlink" title="17.写一函数，将两个字符串连接"></a>17.写一函数，将两个字符串连接</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">char</span> a[N], b[N];</span><br><span class="line">	</span><br><span class="line">	cin &gt;&gt; a;</span><br><span class="line">	cin &gt;&gt; b;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">int</span> len_a = <span class="built_in">strlen</span>(a);</span><br><span class="line">	<span class="keyword">int</span> len_b = <span class="built_in">strlen</span>(b);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> j = len_a,k=<span class="number">0</span>; j &lt; len_a + len_b; j++,k++ )</span><br><span class="line">	&#123;</span><br><span class="line">		a[j] = b[k]; <span class="comment">//后半部分赋新值即可</span></span><br><span class="line">	&#125;	</span><br><span class="line">	cout &lt;&lt; a &lt;&lt;endl;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>


        <h2 id="18-写一函数，输入一个四位数字，要求输出这四个数字字符，但每两个数字间空格。如输入1990，应输出”1-9-9-0”。-谭浩强C语言例题）"   >
          <a href="#18-写一函数，输入一个四位数字，要求输出这四个数字字符，但每两个数字间空格。如输入1990，应输出”1-9-9-0”。-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#18-写一函数，输入一个四位数字，要求输出这四个数字字符，但每两个数字间空格。如输入1990，应输出”1-9-9-0”。-谭浩强C语言例题）" class="headerlink" title="18.写一函数，输入一个四位数字，要求输出这四个数字字符，但每两个数字间空格。如输入1990，应输出”1 9 9 0”。 (谭浩强C语言例题）"></a>18.写一函数，输入一个四位数字，要求输出这四个数字字符，但每两个数字间空格。如输入1990，应输出”1 9 9 0”。 (谭浩强C语言例题）</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//1990	</span></span><br><span class="line">	<span class="keyword">int</span> a;</span><br><span class="line">	stack&lt;<span class="keyword">int</span>&gt; s; <span class="comment">//利用栈stack从个位到最高位 进栈</span></span><br><span class="line">	cin &gt;&gt; a;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">while</span>(a&gt;<span class="number">0</span>)&#123;</span><br><span class="line">		s.<span class="built_in">push</span>(a%<span class="number">10</span>);</span><br><span class="line">		a /= <span class="number">10</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="keyword">while</span>(!s.<span class="built_in">empty</span>())</span><br><span class="line">	&#123;</span><br><span class="line">		cout &lt;&lt; s.<span class="built_in">top</span>() &lt;&lt; <span class="string">&quot; &quot;</span>; <span class="comment">//根据栈的FILO原则 出栈即可</span></span><br><span class="line">		s.<span class="built_in">pop</span>();	</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>


        <h2 id="19-定义一个带参的宏，使两个参数的值互换，并写出程序，输入两个数作为使用宏时的实参。输出已交换后的两个值。"   >
          <a href="#19-定义一个带参的宏，使两个参数的值互换，并写出程序，输入两个数作为使用宏时的实参。输出已交换后的两个值。" class="heading-link"><i class="fas fa-link"></i></a><a href="#19-定义一个带参的宏，使两个参数的值互换，并写出程序，输入两个数作为使用宏时的实参。输出已交换后的两个值。" class="headerlink" title="19.定义一个带参的宏，使两个参数的值互换，并写出程序，输入两个数作为使用宏时的实参。输出已交换后的两个值。"></a>19.定义一个带参的宏，使两个参数的值互换，并写出程序，输入两个数作为使用宏时的实参。输出已交换后的两个值。</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> exchange(a,b) t=a;a=b;b=t;  <span class="comment">//相当于整段代码替换 </span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> a, b, t=<span class="number">0</span>;</span><br><span class="line">	cin &gt;&gt; a &gt;&gt; b;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">exchange</span>(a,b);</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; a &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; b &lt;&lt;endl;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h2 id="20-输入两个整数，求他们相除的余数。用带参的宏来实现，编程序。"   >
          <a href="#20-输入两个整数，求他们相除的余数。用带参的宏来实现，编程序。" class="heading-link"><i class="fas fa-link"></i></a><a href="#20-输入两个整数，求他们相除的余数。用带参的宏来实现，编程序。" class="headerlink" title="20.输入两个整数，求他们相除的余数。用带参的宏来实现，编程序。"></a>20.输入两个整数，求他们相除的余数。用带参的宏来实现，编程序。</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MOD(a,b,res) res = a % b;  <span class="comment">//相当于整段代码替换 </span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> a, b, res=<span class="number">0</span>;</span><br><span class="line">	cin &gt;&gt; a &gt;&gt; b;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">MOD</span>(a,b,res);</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; res &lt;&lt;endl;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h2 id="21-输入一行电报文字，将字母变成其下一字母（如’a’变成’b’……’z’变成’a’其它字符不变）。"   >
          <a href="#21-输入一行电报文字，将字母变成其下一字母（如’a’变成’b’……’z’变成’a’其它字符不变）。" class="heading-link"><i class="fas fa-link"></i></a><a href="#21-输入一行电报文字，将字母变成其下一字母（如’a’变成’b’……’z’变成’a’其它字符不变）。" class="headerlink" title="21.输入一行电报文字，将字母变成其下一字母（如’a’变成’b’……’z’变成’ａ’其它字符不变）。"></a>21.输入一行电报文字，将字母变成其下一字母（如’a’变成’b’……’z’变成’ａ’其它字符不变）。</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">char</span> a[N];</span><br><span class="line">	<span class="built_in">gets</span>(a);  <span class="comment">//输入字符串</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; a[i] != <span class="string">&#x27;\0&#x27;</span>; i++)&#123;</span><br><span class="line">		<span class="keyword">if</span>(a[i] &gt;= <span class="string">&#x27;a&#x27;</span> &amp;&amp; a[i] &lt; <span class="string">&#x27;z&#x27;</span>|| a[i] &gt;= <span class="string">&#x27;A&#x27;</span> &amp;&amp; a[i] &lt; <span class="string">&#x27;Z&#x27;</span>)&#123;</span><br><span class="line">			a[i] += <span class="number">1</span>; </span><br><span class="line">			<span class="comment">//C++与C一样 字符变量都由ASCII码整数存储</span></span><br><span class="line">			<span class="comment">//因而 相应ASCII码自增1即可</span></span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">if</span>(a[i]==<span class="string">&#x27;z&#x27;</span>)</span><br><span class="line">			a[i] = <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">if</span>(a[i]==<span class="string">&#x27;Z&#x27;</span>)</span><br><span class="line">			a[i] = <span class="string">&#x27;A&#x27;</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">puts</span>(a); <span class="comment">//打印字符串</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h2 id="22-现有N个学生的数据记录，每个记录包括学号、姓名、三科成绩。-编写一个函数input-用来输入一个学生的数据记录。-编写一个函数print-打印一个学生的数据记录。-在主函数调用这两个函数，读取N条记录输入，再按要求输出。-N-lt-100"   >
          <a href="#22-现有N个学生的数据记录，每个记录包括学号、姓名、三科成绩。-编写一个函数input-用来输入一个学生的数据记录。-编写一个函数print-打印一个学生的数据记录。-在主函数调用这两个函数，读取N条记录输入，再按要求输出。-N-lt-100" class="heading-link"><i class="fas fa-link"></i></a><a href="#22-现有N个学生的数据记录，每个记录包括学号、姓名、三科成绩。-编写一个函数input-用来输入一个学生的数据记录。-编写一个函数print-打印一个学生的数据记录。-在主函数调用这两个函数，读取N条记录输入，再按要求输出。-N-lt-100" class="headerlink" title="22.现有N个学生的数据记录，每个记录包括学号、姓名、三科成绩。 编写一个函数input,用来输入一个学生的数据记录。 编写一个函数print,打印一个学生的数据记录。 在主函数调用这两个函数，读取N条记录输入，再按要求输出。 N&lt;100"></a>22.现有N个学生的数据记录，每个记录包括学号、姓名、三科成绩。 编写一个函数input,用来输入一个学生的数据记录。 编写一个函数print,打印一个学生的数据记录。 在主函数调用这两个函数，读取N条记录输入，再按要求输出。 N&lt;100</h2>
      <p><img   src="https://img-blog.csdnimg.cn/20210202123009154.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//class:</span></span><br><span class="line"><span class="comment">//私有属性：学号，姓名，三科成绩</span></span><br><span class="line"><span class="comment">//公有方法：void input(); void print();</span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span>&#123;</span> <span class="comment">//类名可以不大写，但是推荐大写 符合Java命名规范</span></span><br><span class="line">	<span class="keyword">public</span>:</span><br><span class="line">		<span class="function"><span class="keyword">void</span> <span class="title">input</span><span class="params">()</span></span>;</span><br><span class="line">		<span class="function"><span class="keyword">void</span> <span class="title">print</span><span class="params">()</span></span>;</span><br><span class="line">		</span><br><span class="line">	<span class="keyword">private</span>:</span><br><span class="line">		string num; </span><br><span class="line">		<span class="comment">//字符串类型 涉及到char 跟 int 的组合 </span></span><br><span class="line">		<span class="comment">//C++中的字符串是string类 Java中是String类</span></span><br><span class="line">		string name;</span><br><span class="line">		<span class="keyword">int</span> Chinese;</span><br><span class="line">		<span class="keyword">int</span> Math;</span><br><span class="line">		<span class="keyword">int</span> English;</span><br><span class="line">&#125;; <span class="comment">//记得有分号； </span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Student::input</span><span class="params">()</span></span>&#123;   <span class="comment">//记得域名符:: 表示实现Student类的方法 </span></span><br><span class="line">	cin &gt;&gt; num &gt;&gt; name &gt;&gt; Chinese &gt;&gt; Math &gt;&gt; English; <span class="comment">//加上域名符直接引用属性 </span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Student::print</span><span class="params">()</span></span>&#123;</span><br><span class="line">	cout &lt;&lt; num &lt;&lt; <span class="string">&quot;,&quot;</span> &lt;&lt; name &lt;&lt; <span class="string">&quot;,&quot;</span> &lt;&lt; Chinese &lt;&lt; <span class="string">&quot;,&quot;</span> &lt;&lt; Math &lt;&lt; <span class="string">&quot;,&quot;</span> &lt;&lt; English &lt;&lt;endl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> n;</span><br><span class="line">	Student stu[N];</span><br><span class="line">	</span><br><span class="line">	cin &gt;&gt; n;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;n; i++)&#123;</span><br><span class="line">		stu[i].<span class="built_in">input</span>();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;n; i++)&#123;</span><br><span class="line">		stu[i].<span class="built_in">print</span>();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h2 id="23-将一个字符串str的内容颠倒过来，并输出。str的长度不超过100个字符。"   >
          <a href="#23-将一个字符串str的内容颠倒过来，并输出。str的长度不超过100个字符。" class="heading-link"><i class="fas fa-link"></i></a><a href="#23-将一个字符串str的内容颠倒过来，并输出。str的长度不超过100个字符。" class="headerlink" title="23.将一个字符串str的内容颠倒过来，并输出。str的长度不超过100个字符。"></a>23.将一个字符串str的内容颠倒过来，并输出。str的长度不超过100个字符。</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">char</span> str[N];</span><br><span class="line">	<span class="built_in">gets</span>(str);</span><br><span class="line">	<span class="keyword">int</span> len = <span class="built_in">strlen</span>(str);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=len<span class="number">-1</span>; i&gt;=<span class="number">0</span>; i--)&#123; </span><br><span class="line">	<span class="comment">//利用for循环颠倒该数组即可</span></span><br><span class="line">	<span class="comment">//由于C++中最后一个字符是&#x27;\0&#x27; 因此从倒数第二个字符开始自减</span></span><br><span class="line">		cout &lt;&lt; str[i];</span><br><span class="line">	&#125; </span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>

        <h2 id="24-解一元二次方程-谭浩强C语言例题）"   >
          <a href="#24-解一元二次方程-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#24-解一元二次方程-谭浩强C语言例题）" class="headerlink" title="24.解一元二次方程 (谭浩强C语言例题）"></a>24.解一元二次方程 (谭浩强C语言例题）</h2>
      <p><img   src="https://img-blog.csdnimg.cn/20210206145605924.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line"><span class="comment">//	a*x^2+b*x+c=0 </span></span><br><span class="line">	<span class="keyword">int</span> a, b, c;</span><br><span class="line">	<span class="keyword">double</span> res1, res2;</span><br><span class="line">	cin &gt;&gt; a &gt;&gt; b &gt;&gt; c;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">double</span> delta = <span class="built_in">pow</span>(b,<span class="number">2</span>) - <span class="number">4</span> * a * c;</span><br><span class="line">	<span class="keyword">if</span>(delta == <span class="number">0</span>)&#123;</span><br><span class="line">		res1 = (-b + <span class="built_in">sqrt</span>(delta) ) / <span class="number">2</span> * a;</span><br><span class="line">		res2 = res1;</span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">		res1 = (-b + <span class="built_in">sqrt</span>(delta) ) / <span class="number">2</span> * a;</span><br><span class="line">		res2 = (-b - <span class="built_in">sqrt</span>(delta) ) / <span class="number">2</span> * a;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%.2f %.2f&quot;</span>, res1, res2);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>


        <h2 id="25-输入字符串-将组成字符的所有非英文字母的字符删除后输出"   >
          <a href="#25-输入字符串-将组成字符的所有非英文字母的字符删除后输出" class="heading-link"><i class="fas fa-link"></i></a><a href="#25-输入字符串-将组成字符的所有非英文字母的字符删除后输出" class="headerlink" title="25.输入字符串 将组成字符的所有非英文字母的字符删除后输出"></a>25.输入字符串 将组成字符的所有非英文字母的字符删除后输出</h2>
      <p><img   src="https://img-blog.csdnimg.cn/20210206151824180.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//a ASCII是97</span></span><br><span class="line">	<span class="keyword">char</span> str[N]; <span class="comment">//定义所输入的字符数组</span></span><br><span class="line">	<span class="built_in">gets</span>(str); 	<span class="comment">//输入该数组的数据</span></span><br><span class="line">	<span class="keyword">int</span> len = <span class="built_in">strlen</span>(str);</span><br><span class="line">	</span><br><span class="line">	vector&lt;<span class="keyword">char</span>&gt; v; <span class="comment">//定义vector模板类</span></span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;len; i++)&#123;</span><br><span class="line">		<span class="keyword">if</span>(str[i] &lt; <span class="string">&#x27;A&#x27;</span>|| str[i] &gt;<span class="string">&#x27;Z&#x27;</span>&amp;&amp; str[i] &lt;<span class="string">&#x27;a&#x27;</span> || str[i]&gt;<span class="string">&#x27;z&#x27;</span>)&#123;  </span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			<span class="comment">//所输入数据是大小写字母时 存入动态数组vector中</span></span><br><span class="line">			v.<span class="built_in">push_back</span>(str[i]);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i &lt; v.<span class="built_in">size</span>(); i++)&#123;</span><br><span class="line">		cout &lt;&lt; v[i];</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>



        <h2 id="谭浩强第四版课后习题答案："   >
          <a href="#谭浩强第四版课后习题答案：" class="heading-link"><i class="fas fa-link"></i></a><a href="#谭浩强第四版课后习题答案：" class="headerlink" title="谭浩强第四版课后习题答案："></a>谭浩强第四版课后习题答案：</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br></pre></td><td class="code"><pre><span class="line">### 第二章：</span><br><span class="line"><span class="number">2.4</span></span><br><span class="line">#<span class="number">1</span>、以参数形式交换两个数（记得参数加引用）</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">swap</span><span class="params">(<span class="keyword">int</span> &amp;a, <span class="keyword">int</span> &amp;b)</span></span>&#123; </span><br><span class="line"><span class="comment">//什么时候用引用？ 当需要将操作后的值 传递回去</span></span><br><span class="line">	<span class="keyword">int</span> t = a;</span><br><span class="line">	a = b;</span><br><span class="line">	b = t;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> num1, num2;</span><br><span class="line">	cin &gt;&gt; num1 &gt;&gt; num2;</span><br><span class="line">	<span class="built_in">swap</span>(num1, num2);</span><br><span class="line">	cout &lt;&lt; num1 &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; num2;	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">#<span class="number">2.</span>依次输入<span class="number">5</span>个值 要求输入其中的最大值</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> L 5</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> a[L],max;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;L; i++)&#123;</span><br><span class="line">		cin &gt;&gt; a[i];</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	max = a[<span class="number">0</span>];</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>; i&lt;=<span class="number">5</span>; i++)&#123;</span><br><span class="line">		<span class="keyword">if</span>(max &lt; a[i])</span><br><span class="line">			max = a[i];</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;Max is:&quot;</span> &lt;&lt; max &lt;&lt; endl;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">#<span class="number">3.</span>有三个数a,b,c 要求输出最大值</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> L 5</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> num1, num2, num3;</span><br><span class="line">	<span class="keyword">int</span> max;</span><br><span class="line">	cin &gt;&gt; num1 &gt;&gt; num2 &gt;&gt; num3;</span><br><span class="line">	max = (num1 &gt; num2)? num1 : num2; <span class="comment">//第一个数与第二个数比较 结果是max</span></span><br><span class="line">	max = (max &gt; num3)? max : num3;	  <span class="comment">//max与第三个数比较 重新赋max的值</span></span><br><span class="line">	cout &lt;&lt; max &lt;&lt;endl; </span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">#<span class="number">4.</span>求<span class="number">1</span>+<span class="number">2</span>+<span class="number">3</span>+...+<span class="number">100</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span> </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> i, sum=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span>(i&lt;=<span class="number">100</span>)&#123;</span><br><span class="line">		sum += i;</span><br><span class="line">		i++;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; sum &lt;&lt;endl;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">#<span class="number">5.</span>判断一个数是否同时被<span class="number">3</span>和<span class="number">5</span>整除</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span> </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> num;</span><br><span class="line">	cin &gt;&gt; num;</span><br><span class="line">	<span class="keyword">if</span>(num%<span class="number">3</span>==<span class="number">0</span> &amp;&amp; num%<span class="number">5</span> ==<span class="number">0</span>)&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;该数%d可以被3和5整除&quot;</span>, num);</span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;不行&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">#<span class="number">6.</span>输出<span class="number">100</span>~<span class="number">200</span>之间的素数</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span> </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">fac</span><span class="params">(<span class="keyword">int</span> x)</span></span>&#123;</span><br><span class="line">	<span class="keyword">bool</span> flag = <span class="literal">true</span>;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>; i&lt;x/<span class="number">2</span>; i++)&#123; <span class="comment">//注意 对素数的判断 是需要判断2~n/2即可</span></span><br><span class="line">		<span class="keyword">if</span>(x%i==<span class="number">0</span>)&#123;</span><br><span class="line">			flag = <span class="literal">false</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> flag;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">100</span>; i&lt;=<span class="number">200</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span>(<span class="built_in">fac</span>(i) == <span class="literal">true</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;%d是素数\n&quot;</span>, i);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">#<span class="number">7</span> 求两个数的最大公约数</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span> </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">fun</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span></span>&#123; <span class="comment">//传参进来a大于b </span></span><br><span class="line">	<span class="keyword">int</span> c = a%b; </span><br><span class="line">	<span class="comment">//辗转相除法 若余数为0 则小者为最大公约数</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">while</span>(c != <span class="number">0</span>)&#123; <span class="comment">//如果余数不为0 </span></span><br><span class="line">		a = b; <span class="comment">//否则将余数作为除数  </span></span><br><span class="line">		b = c; <span class="comment">//小者作为被除数</span></span><br><span class="line">		c = a % b;		</span><br><span class="line">	&#125;</span><br><span class="line">	cout &lt;&lt; b &lt;&lt;<span class="string">&quot;是最大公约数&quot;</span> &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">swap</span><span class="params">(<span class="keyword">int</span> &amp;a, <span class="keyword">int</span> &amp;b)</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> t = a;</span><br><span class="line">	a = b;</span><br><span class="line">	b = t;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> num1, num2;</span><br><span class="line">	cin &gt;&gt; num1 &gt;&gt; num2;</span><br><span class="line">	<span class="keyword">if</span>(num1 &lt; num2)</span><br><span class="line">		<span class="built_in">swap</span>(num1, num2); <span class="comment">//对大小相反做处理 交换即可</span></span><br><span class="line">	<span class="built_in">fun</span>(num1, num2);	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">#<span class="number">8.</span> 求一元二次方程的根 </span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span> </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//ax^2 + bx + c</span></span><br><span class="line">	<span class="keyword">double</span> a, b, c;</span><br><span class="line">	cin &gt;&gt; a &gt;&gt; b &gt;&gt; c;</span><br><span class="line">	<span class="keyword">double</span> delt = <span class="built_in">pow</span>(b,<span class="number">2</span>) - <span class="number">4</span> * a * c;</span><br><span class="line">	<span class="keyword">double</span> x1 = (-b+<span class="built_in">sqrt</span>(delt))/(<span class="number">2</span>*a);</span><br><span class="line">	<span class="keyword">double</span> x2 = (-b-<span class="built_in">sqrt</span>(delt))/(<span class="number">2</span>*a);</span><br><span class="line">		</span><br><span class="line">	<span class="keyword">if</span>(delt &gt; <span class="number">0</span>)&#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;实根分别为：&quot;</span> &lt;&lt; x1 &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; x2 &lt;&lt;endl;</span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(delt == <span class="number">0</span>)&#123;</span><br><span class="line">			cout &lt;&lt; <span class="string">&quot;实根为:&quot;</span> &lt;&lt; x1 &lt;&lt; endl;			</span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			cout &lt;&lt; <span class="string">&quot;无实根&quot;</span> &lt;&lt; endl; </span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>

        <h2 id="第三章"   >
          <a href="#第三章" class="heading-link"><i class="fas fa-link"></i></a><a href="#第三章" class="headerlink" title="第三章"></a>第三章</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">第三章：</span><br><span class="line">#<span class="number">3</span><span class="number">-5</span>：</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">int</span> a,b;</span><br><span class="line">	<span class="keyword">float</span> x;</span><br><span class="line">	<span class="keyword">double</span> y;</span><br><span class="line">	<span class="keyword">char</span> c1, c2;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;input the Integer a,b:\n&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;a, &amp;b);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;input the Float x:\n&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%f&quot;</span>,&amp;x);</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;input the Double y:\n&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%lf&quot;</span>,&amp;y);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;input the char c1:\n&quot;</span>);</span><br><span class="line">	c1 = <span class="built_in">getchar</span>();</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;input the char c2:\n&quot;</span>);</span><br><span class="line">	c2 = <span class="built_in">getchar</span>();</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d,%d,%.2f,%.2lf,%c,%c&quot;</span>,a,b,x,y,c1,c2);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#<span class="number">3.6</span>加密 将China译成密码 利用<span class="keyword">char</span>字符在计算机中存储ASCII码值 每个字符相加四个单位即可</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">char</span> c1,c2,c3,c4,c5;</span><br><span class="line">	c1 = <span class="string">&#x27;C&#x27;</span>;</span><br><span class="line">	c2 = <span class="string">&#x27;h&#x27;</span>;</span><br><span class="line">	c3 = <span class="string">&#x27;i&#x27;</span>;</span><br><span class="line">	c4 = <span class="string">&#x27;n&#x27;</span>;</span><br><span class="line">	c5 = <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line"></span><br><span class="line">	c1+=<span class="number">4</span>;</span><br><span class="line">	c2+=<span class="number">4</span>;</span><br><span class="line">	c3+=<span class="number">4</span>;</span><br><span class="line">	c4+=<span class="number">4</span>;</span><br><span class="line">	c5+=<span class="number">4</span>;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">putchar</span>(c1);</span><br><span class="line">	<span class="built_in">putchar</span>(c2);</span><br><span class="line">	<span class="built_in">putchar</span>(c3);</span><br><span class="line">	<span class="built_in">putchar</span>(c4);</span><br><span class="line">	<span class="built_in">putchar</span>(c5);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%c%c%c%c%c&quot;</span>,c1,c2,c3,c4,c5);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="第五章"   >
          <a href="#第五章" class="heading-link"><i class="fas fa-link"></i></a><a href="#第五章" class="headerlink" title="第五章"></a>第五章</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br></pre></td><td class="code"><pre><span class="line">第五章：</span><br><span class="line"><span class="number">5.3</span> 求最大公约数与最小公倍数</span><br><span class="line"></span><br><span class="line"><span class="number">5.3</span><span class="number">.1</span>:最大公约数：</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span> </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//求a b两个数字的最大公约数 （辗转相除法） </span></span><br><span class="line">	<span class="keyword">int</span> a, b, res = <span class="number">0</span>;</span><br><span class="line">	cin &gt;&gt; a &gt;&gt; b;</span><br><span class="line">	<span class="keyword">if</span>(a &lt; b) <span class="comment">//默认a&gt;b</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">int</span> t = a;</span><br><span class="line">		a = b;</span><br><span class="line">		b = t;</span><br><span class="line">	&#125; </span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span>(a % b == <span class="number">0</span>)&#123;  <span class="comment">//如果a是b的倍数 那么最大公因数就是b </span></span><br><span class="line">		res = b; </span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">		<span class="keyword">int</span> c = a % b;	<span class="comment">//否则先取余 余数在else语句中肯定是不为0的 </span></span><br><span class="line">		<span class="keyword">while</span>(c != <span class="number">0</span>)&#123;	<span class="comment">//若若满足不为0 则较小数作为a 余数作为b c重新被a b取余 </span></span><br><span class="line">			a = b; </span><br><span class="line">			b = c;</span><br><span class="line">			c = a % b;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		res = b;</span><br><span class="line">	&#125;</span><br><span class="line">	cout &lt;&lt; res;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">5.3</span><span class="number">.2</span>：最小公倍数：</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span> </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//求a b两个数字的最小公倍数</span></span><br><span class="line">	<span class="keyword">int</span> a,b,res=<span class="number">0</span>;</span><br><span class="line">	</span><br><span class="line">	cin &gt;&gt; a &gt;&gt; b;</span><br><span class="line">	<span class="keyword">if</span>(a &lt; b)&#123; <span class="comment">//默认a大于b </span></span><br><span class="line">		<span class="keyword">int</span> t = a;</span><br><span class="line">		a = b;</span><br><span class="line">		b = t;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span>(a % b == <span class="number">0</span>)&#123; <span class="comment">//如果a是b的倍数 那么最小公倍数就是a </span></span><br><span class="line">		res = a; </span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">		<span class="keyword">int</span> temp=b;</span><br><span class="line">		<span class="keyword">while</span>(temp&lt;=a*b)&#123; <span class="comment">//直到temp变成a*b </span></span><br><span class="line">			<span class="keyword">if</span>(temp%a==<span class="number">0</span>&amp;&amp;temp%b==<span class="number">0</span>)&#123; <span class="comment">//如果从较小数开始 一直自增 如果能同时取余两个数 那么就是公倍数 </span></span><br><span class="line">				res = temp;</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			temp++;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; res &lt;&lt;endl;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">5.8</span> 输出所有水仙花数(三位数，且各数的立方和等于该数本身)：</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span> </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isNum</span><span class="params">(<span class="keyword">int</span> num)</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line">	<span class="function">vector&lt;<span class="keyword">int</span>&gt; <span class="title">v</span><span class="params">(<span class="number">1</span>, num)</span></span>;</span><br><span class="line">	vector&lt;<span class="keyword">int</span>&gt;::iterator it;</span><br><span class="line">	<span class="keyword">bool</span> flag = <span class="literal">false</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">while</span>(num)&#123;</span><br><span class="line">		v.<span class="built_in">push_back</span>(num%<span class="number">10</span>);</span><br><span class="line">		num /= <span class="number">10</span>;</span><br><span class="line">	&#125; </span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(it=v.<span class="built_in">begin</span>()+<span class="number">1</span>; it != v.<span class="built_in">end</span>(); it++)&#123;</span><br><span class="line">		sum += (<span class="built_in">pow</span>(*it, <span class="number">3</span>));</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span>(sum == v[<span class="number">0</span>])&#123;</span><br><span class="line">		flag = <span class="literal">true</span>;</span><br><span class="line">	&#125;	</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> flag;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//求所有的水仙花 </span></span><br><span class="line">	<span class="keyword">int</span> num;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i =<span class="number">100</span>; i&lt;<span class="number">1000</span>; i++)&#123;</span><br><span class="line">		<span class="keyword">if</span>(<span class="built_in">isNum</span>(i))&#123;</span><br><span class="line">			cout &lt;&lt; i &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">		&#125;	</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">5.9</span>求<span class="number">1000</span>之内的所有完数</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span> </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">getNum</span><span class="params">(<span class="keyword">int</span> num)</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> sum=<span class="number">0</span>; </span><br><span class="line">	<span class="function">vector&lt;<span class="keyword">int</span>&gt; <span class="title">v</span><span class="params">(<span class="number">1</span>, num)</span></span>;</span><br><span class="line">	vector&lt;<span class="keyword">int</span>&gt;::iterator it;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>; i&lt;num; i++)&#123; <span class="comment">//千万注意 取余的两边绝对！绝对不能为0 因此i不能是0 </span></span><br><span class="line">		<span class="keyword">if</span>(num%i==<span class="number">0</span>)</span><br><span class="line">			v.<span class="built_in">push_back</span>(i);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(it=v.<span class="built_in">begin</span>()+<span class="number">1</span>; it!=v.<span class="built_in">end</span>(); it++)&#123;</span><br><span class="line">		sum += (*it);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span>(sum == v[<span class="number">0</span>])&#123;</span><br><span class="line">		cout &lt;&lt; sum &lt;&lt; <span class="string">&quot;是完数&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">	v.<span class="built_in">clear</span>();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//求完数 即一个数等于它所有的因子之和</span></span><br><span class="line">	<span class="keyword">int</span> num,end = <span class="number">1000</span>;</span><br><span class="line">	<span class="keyword">while</span>(num++ &lt; end)&#123;</span><br><span class="line">		<span class="built_in">getNum</span>(num);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">5.10</span>：求 <span class="number">2</span>/<span class="number">1</span> + <span class="number">3</span>/<span class="number">2</span> + <span class="number">5</span>/<span class="number">3</span> +... 数列的前<span class="number">20</span>项和</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span> </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">double</span> up=<span class="number">2</span>, down=<span class="number">1</span>,res = <span class="number">0</span>; <span class="comment">//up代表分母 down代表分子 </span></span><br><span class="line">	<span class="keyword">int</span> i=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span>(i++&lt;<span class="number">20</span>)&#123;</span><br><span class="line">		res += (up/down);</span><br><span class="line">		up = up + down;</span><br><span class="line">		down = up - down; <span class="comment">//注意 这里不能用down = up  因为up 已经是 up+down </span></span><br><span class="line">						  <span class="comment">//而应该是新的up减去之前的down </span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; res;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">5.11</span>: 一个球从<span class="number">100</span>M高度落下 每次落地反弹回原高度的一般 再落下...求第<span class="number">10</span>次 共经过多少米 反弹多高？</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="built_in">main</span>()</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">float</span> height=<span class="number">100.0</span>,sum_height=<span class="number">100</span>;</span><br><span class="line"><span class="keyword">int</span> i;</span><br><span class="line"><span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;<span class="number">10</span>;i++) &#123;</span><br><span class="line">	height=height/<span class="number">2</span>;</span><br><span class="line">	sum_height+=height*<span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">&quot;%fm\n&quot;</span>,sum_height);</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">&quot;%fm\n&quot;</span>,height);</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="第六章："   >
          <a href="#第六章：" class="heading-link"><i class="fas fa-link"></i></a><a href="#第六章：" class="headerlink" title="第六章："></a>第六章：</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line">第六章：</span><br><span class="line">	例题：输入字符串 从中打印出单词的个数</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"><span class="built_in">main</span>()</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">char</span> str[N];</span><br><span class="line">	<span class="keyword">int</span> num,word=<span class="number">0</span>;</span><br><span class="line">	<span class="built_in">gets</span>(str);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; str[i]!=<span class="string">&#x27;\0&#x27;</span>; i++)&#123;</span><br><span class="line">		<span class="keyword">if</span>(str[i] == <span class="string">&#x27; &#x27;</span>)&#123; <span class="comment">//如果一开始就是空格 那么不是单词 </span></span><br><span class="line">			word = <span class="number">0</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span> <span class="keyword">if</span>(word==<span class="number">0</span>)&#123;	<span class="comment">//空格之后 认为是单词 记为遇到1个 </span></span><br><span class="line">			word = <span class="number">1</span>;</span><br><span class="line">			num++;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; num &lt;&lt;endl; </span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">6.5</span>：逆序存放升序的数组 比如处理前：<span class="number">1</span> <span class="number">2</span> <span class="number">3</span> <span class="number">4</span> <span class="number">5</span> 存放后为：<span class="number">5</span> <span class="number">4</span> <span class="number">3</span> <span class="number">2</span> <span class="number">1</span> </span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"><span class="built_in">main</span>()</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">int</span> arr[N],len;</span><br><span class="line">	stack&lt;<span class="keyword">int</span>&gt; s;</span><br><span class="line">	</span><br><span class="line">	cin &gt;&gt; len;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;len; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		cin &gt;&gt; arr[i];</span><br><span class="line">		s.<span class="built_in">push</span>(arr[i]);  <span class="comment">//利用栈的FILO原理 从头到尾将元素进行入栈</span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;处理前的数组元素分别为：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i =<span class="number">0</span>; i&lt;len; i++)</span><br><span class="line">		cout &lt;&lt; arr[i] &lt;&lt; <span class="string">&quot; &quot;</span>; 		</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; endl;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;len; i++)&#123;</span><br><span class="line">		arr[i] = s.<span class="built_in">top</span>();</span><br><span class="line">		s.<span class="built_in">pop</span>();</span><br><span class="line">	&#125;	</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;处理后的数组元素分别为：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i =<span class="number">0</span>; i&lt;len; i++)</span><br><span class="line">		cout &lt;&lt; arr[i] &lt;&lt; <span class="string">&quot; &quot;</span>; 		</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">6.9</span>：</span><br><span class="line">有若干个数字在有序数组中 此时要求实现折半查找方式来查找某个元素</span><br><span class="line">并给出它的位置 若找不到该数 则输出 无此数</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> a[<span class="number">6</span>] = &#123;<span class="number">1</span>, <span class="number">4</span>, <span class="number">6</span>, <span class="number">32</span>, <span class="number">58</span>, <span class="number">66</span>&#125;;</span><br><span class="line">	<span class="keyword">int</span> left = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">int</span> right = <span class="number">5</span>; <span class="comment">//注意是最后一个元素 即 长度-1</span></span><br><span class="line">	<span class="keyword">int</span> mid,num;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;输入欲查找的数字：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	cin &gt;&gt; num;</span><br><span class="line">	<span class="keyword">while</span>(left &lt;= right)&#123; <span class="comment">//注意条件</span></span><br><span class="line">		mid = (left + right) / <span class="number">2</span>;</span><br><span class="line">		<span class="keyword">if</span>(a[mid] == num)&#123;</span><br><span class="line">			cout &lt;&lt; <span class="string">&quot;已找到，是第&quot;</span> &lt;&lt; mid+<span class="number">1</span> &lt;&lt; <span class="string">&quot;个元素&quot;</span> &lt;&lt;endl;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span> <span class="keyword">if</span>(a[mid] &lt; num)&#123; </span><br><span class="line">			left = mid + <span class="number">1</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			right = mid - <span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span>(left &gt; right)&#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;无此数&quot;</span> &lt;&lt;endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">统计各字符</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">char</span> str[<span class="number">2</span>][<span class="number">10</span>];</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i =<span class="number">0</span>; i&lt;<span class="number">2</span>; i++)</span><br><span class="line">		<span class="built_in">gets</span>(str[i]);      <span class="comment">//for循环每次输入该行</span></span><br><span class="line"></span><br><span class="line">	cout &lt;&lt; endl;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//统计大写字母 小写字母 数字 空格以及其他字符 </span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">int</span> num=<span class="number">0</span>, U_char=<span class="number">0</span>, L_char=<span class="number">0</span>, space=<span class="number">0</span>, oth=<span class="number">0</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i =<span class="number">0</span>; i&lt;<span class="number">2</span>; i++) &#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>; j&lt;<span class="number">10</span>; j++)&#123;</span><br><span class="line">			<span class="keyword">char</span> ch= str[i][j];</span><br><span class="line">			<span class="keyword">if</span>(ch &gt;= <span class="string">&#x27;0&#x27;</span> &amp;&amp; ch &lt;= <span class="string">&#x27;9&#x27;</span> )&#123;</span><br><span class="line">				num++;				</span><br><span class="line">			&#125;<span class="keyword">else</span> <span class="keyword">if</span>(ch &gt;= <span class="string">&#x27;A&#x27;</span> &amp;&amp; ch &lt;= <span class="string">&#x27;Z&#x27;</span>)&#123;</span><br><span class="line">				U_char++;</span><br><span class="line">			&#125;<span class="keyword">else</span> <span class="keyword">if</span>(ch &gt;= <span class="string">&#x27;a&#x27;</span> &amp;&amp; ch &lt;= <span class="string">&#x27;z&#x27;</span>)&#123;</span><br><span class="line">				L_char++;</span><br><span class="line">			&#125;<span class="keyword">else</span> <span class="keyword">if</span>(ch == <span class="string">&#x27; &#x27;</span>)&#123;</span><br><span class="line">				space++;</span><br><span class="line">			&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">				oth++;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;数字：&quot;</span> &lt;&lt; num&lt;&lt;endl;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;大写字母：&quot;</span> &lt;&lt; U_char &lt;&lt;endl;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;小写字母:&quot;</span> &lt;&lt; L_char &lt;&lt; endl;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;空格：&quot;</span> &lt;&lt;space &lt;&lt;endl;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;其他：&quot;</span> &lt;&lt; oth &lt;&lt;endl;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">6.14</span></span><br><span class="line">比较两个字符串 其中返回正数 该正数表示相差的总值</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="comment">//两个字符串的比较 返回总相差的ASCII码值</span></span><br><span class="line">	<span class="keyword">char</span> c1[N], c2[N];</span><br><span class="line">	<span class="keyword">int</span> len1, len2;</span><br><span class="line">	<span class="keyword">int</span> sum=<span class="number">0</span>;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;请输入字符串一：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	<span class="built_in">gets</span>(c1);</span><br><span class="line">	len1 = <span class="built_in">strlen</span>(c1);</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;请输入字符串二：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	<span class="built_in">gets</span>(c2);</span><br><span class="line">	len2 = <span class="built_in">strlen</span>(c2);</span><br><span class="line">	<span class="keyword">int</span> len = (len1&gt;len2)? len1: len2;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;len; i++)&#123;</span><br><span class="line">	   sum += <span class="built_in">abs</span>(c1[i]-c2[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	 </span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;两者相差：&quot;</span> &lt;&lt; sum &lt;&lt;endl;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">6.15</span> 将字符数组a2的所有字符都复制到a1中去 其中包括a2的结束字符<span class="string">&#x27;\0&#x27;</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="comment">//两个字符串的比较 返回总相差的ASCII码值</span></span><br><span class="line">	<span class="keyword">char</span> c1[N], c2[N]=&#123;<span class="string">&#x27;v&#x27;</span>,<span class="string">&#x27;i&#x27;</span>,<span class="string">&#x27;n&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;\0&#x27;</span>&#125;;</span><br><span class="line">	<span class="keyword">int</span> len1, len2;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;请输入字符串一：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	<span class="built_in">gets</span>(c1);</span><br><span class="line">	len1 = <span class="built_in">strlen</span>(c1);</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;请输入字符串二：&quot;</span> &lt;&lt;endl;</span><br><span class="line"></span><br><span class="line">	len2 = <span class="built_in">strlen</span>(c2)+<span class="number">1</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> k = len1,i=<span class="number">0</span>; k&lt;len1+len2; k++,i++)&#123;</span><br><span class="line">		c1[k] = c2[i];</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> k = <span class="number">0</span>; k&lt;len1+len2; k++)&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%c它的ASCII值为%d\n&quot;</span>, c1[k],c1[k]);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="第七章："   >
          <a href="#第七章：" class="heading-link"><i class="fas fa-link"></i></a><a href="#第七章：" class="headerlink" title="第七章："></a>第七章：</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="number">7.10</span>：写一个函数，输入一行字符，将最长的单词输出：</span><br><span class="line">【转】：https:<span class="comment">//blog.csdn.net/navicheung/article/details/77990479</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="comment">//最长单词函数</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">getLongWord</span><span class="params">(<span class="keyword">char</span> str[])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> max_length=<span class="number">0</span>, len=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">char</span> temp[N], res[N];</span><br><span class="line">    <span class="keyword">int</span> n = <span class="built_in">strlen</span>(str);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;=n; i++)&#123;</span><br><span class="line">    	</span><br><span class="line">		<span class="comment">//如果字符是单词的一部分 加入到temp数组 </span></span><br><span class="line">        <span class="keyword">if</span> ((str[i]&gt;=<span class="string">&#x27;a&#x27;</span>&amp;&amp;str[i]&lt;=<span class="string">&#x27;z&#x27;</span>)||(str[i]&gt;=<span class="string">&#x27;A&#x27;</span>&amp;&amp;str[i]&lt;=<span class="string">&#x27;Z&#x27;</span>)) </span><br><span class="line">            temp[len++]=str[i];</span><br><span class="line">        <span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="comment">//若不是单词 那么temp数组不再增添元素 而是结束 </span></span><br><span class="line">			temp[len]=<span class="string">&#x27;\0&#x27;</span>;</span><br><span class="line">            </span><br><span class="line">            <span class="comment">//如果每次长度有更长的 更新 且记得len重新赋值为0 以下一次判断 </span></span><br><span class="line">			<span class="comment">//最终的结果存入ma数组中 关键语句 </span></span><br><span class="line">            len&gt;max_length ? max_length=len, <span class="built_in">strcpy</span>(res, temp), len=<span class="number">0</span> : len=<span class="number">0</span>; </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;最长的单词是： &quot;</span>);</span><br><span class="line">    <span class="built_in">puts</span>(res);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">char</span> str[N];</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;请输入字符串：\n &quot;</span>);       <span class="comment">//输入一行字符</span></span><br><span class="line">    <span class="built_in">gets</span>(str);</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">getLongWord</span>(str);                         <span class="comment">//调用最长单词函数</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">7.11</span> 利用起泡法对输入的n个字符进行升序排序：</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">swap</span><span class="params">(<span class="keyword">char</span> &amp;a, <span class="keyword">char</span> &amp;b)</span></span>&#123;</span><br><span class="line">	<span class="keyword">char</span> t = a;</span><br><span class="line">	a = b;</span><br><span class="line">	b = t;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">char</span> str[N];</span><br><span class="line">   	<span class="keyword">int</span> length;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;你要输入几个字符：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	cin &gt;&gt; length;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;依次输入这些字符：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;length; i++)&#123;</span><br><span class="line">		cin &gt;&gt; str[i];</span><br><span class="line">	&#125;</span><br><span class="line">	 </span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;length; i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>; j&lt;length-i<span class="number">-1</span>; j++)&#123;</span><br><span class="line">			<span class="keyword">if</span>(str[j] &gt; str[j+<span class="number">1</span>])&#123;</span><br><span class="line">				<span class="built_in">swap</span>(str[j], str[j+<span class="number">1</span>]);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;起泡法排序结果：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;length; i++)</span><br><span class="line">		cout &lt;&lt; str[i] &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">7.16</span> 输入一个十六进制数字（字符串的形式），输出相应的十进制数字：</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="comment">//十六进制数转换为十进制数</span></span><br><span class="line"><span class="comment">//相应的位置 按权展开即可</span></span><br><span class="line"><span class="comment">//A2 = A*10^1 + 2*10^0 </span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">fun</span><span class="params">(<span class="keyword">char</span> str[])</span></span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">int</span> n = <span class="built_in">strlen</span>(str); <span class="comment">//n代表字符串的长度 即代表十六进制数是n位数 </span></span><br><span class="line">	<span class="keyword">int</span> sum =<span class="number">0</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=n<span class="number">-1</span>,j=<span class="number">0</span>; i&gt;=<span class="number">0</span>; i--)&#123;</span><br><span class="line">		<span class="keyword">int</span> temp = <span class="number">0</span>;</span><br><span class="line">		<span class="keyword">if</span>(str[i]==<span class="string">&#x27;A&#x27;</span>)&#123;</span><br><span class="line">			temp = <span class="number">10</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span> <span class="keyword">if</span>(str[i]==<span class="string">&#x27;B&#x27;</span>)&#123;</span><br><span class="line">			temp = <span class="number">11</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span> <span class="keyword">if</span>(str[i]==<span class="string">&#x27;C&#x27;</span>)&#123;</span><br><span class="line">			temp = <span class="number">12</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span> <span class="keyword">if</span>(str[i]==<span class="string">&#x27;D&#x27;</span>)&#123;</span><br><span class="line">			temp = <span class="number">13</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span> <span class="keyword">if</span>(str[i]==<span class="string">&#x27;E&#x27;</span>)&#123;</span><br><span class="line">			temp = <span class="number">14</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span> <span class="keyword">if</span>(str[i]==<span class="string">&#x27;F&#x27;</span>)&#123;</span><br><span class="line">			temp = <span class="number">15</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			temp = str[i] - <span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		sum += (temp * <span class="built_in">pow</span>(<span class="number">16</span>, j++));</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> sum;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">char</span> str[N];</span><br><span class="line">    <span class="built_in">gets</span>(str);</span><br><span class="line">    </span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;十进制数是：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	cout &lt;&lt; <span class="built_in">fun</span>(str) &lt;&lt;endl;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">7.17</span> 递归法 将一个整数转换为字符串</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="comment">//将一个整数 转换成字符串 </span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">fun</span><span class="params">(<span class="keyword">int</span> n)</span></span>&#123;</span><br><span class="line">	<span class="keyword">char</span> ch; <span class="comment">//每次输出的字符 </span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">//1.递归的第一步 首先想好边界情况 这里每次i位数每次取i-1 直到结果为0 </span></span><br><span class="line">	<span class="keyword">if</span>(n==<span class="number">0</span>)&#123;  </span><br><span class="line">		<span class="keyword">return</span>;	</span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;		</span><br><span class="line">	<span class="comment">//2.否则 执行每一次的操作具体的行为</span></span><br><span class="line">	<span class="comment">//注意这里fun一直到边界才输出！因此结果是12（假如n=12）</span></span><br><span class="line">	<span class="comment">//如果fun放在末尾 那么会先输出数字 因而结果会是21 </span></span><br><span class="line">		<span class="built_in">fun</span>(n/<span class="number">10</span>);</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%c&quot;</span>, (n%<span class="number">10</span>) + <span class="string">&#x27;0&#x27;</span>);   				</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> num =<span class="number">0</span>;</span><br><span class="line">	cin &gt;&gt; num;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">fun</span>(num);</span><br><span class="line">	</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">6.18</span>：给出年、月、日 计算是该年的第几天</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isLeapYear</span><span class="params">(<span class="keyword">int</span> year)</span></span>&#123;</span><br><span class="line">	<span class="keyword">bool</span> flag = <span class="literal">false</span>;</span><br><span class="line">	<span class="keyword">if</span>( year%<span class="number">400</span>==<span class="number">0</span> || (year%<span class="number">4</span>==<span class="number">0</span>&amp;&amp;year%<span class="number">100</span>!=<span class="number">0</span>) )&#123;  </span><br><span class="line">	<span class="comment">//1.被400整除的年份是闰年</span></span><br><span class="line">	<span class="comment">//2.被4整除同时不能被100所整除的年份是闰年 </span></span><br><span class="line">		flag = <span class="literal">true</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> flag;	</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//输入某年某月某日 判断这是该年中的第几天</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">fun</span><span class="params">(<span class="keyword">int</span> year, <span class="keyword">int</span> month, <span class="keyword">int</span> day)</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> months[<span class="number">12</span>] = &#123;<span class="number">31</span>, <span class="number">28</span>, <span class="number">31</span>, <span class="number">30</span>, <span class="number">31</span>, <span class="number">30</span>, <span class="number">31</span>, <span class="number">31</span>, <span class="number">30</span>, <span class="number">31</span>, <span class="number">30</span>, <span class="number">31</span>&#125;; <span class="comment">//非闰年的各月份天数数组 </span></span><br><span class="line">	<span class="keyword">int</span> days_sum = <span class="number">0</span>,i=<span class="number">0</span>; </span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span>(month&lt;<span class="number">2</span>)&#123; <span class="comment">//如果没到2月 则不用计算是不是闰年 </span></span><br><span class="line">		days_sum = day;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;该日期是第&quot;</span> &lt;&lt; days_sum &lt;&lt; <span class="string">&quot;天&quot;</span> &lt;&lt;endl; </span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(<span class="built_in">isLeapYear</span>(year))&#123;</span><br><span class="line">			months[<span class="number">1</span>] = <span class="number">29</span>; <span class="comment">//闰年2月有29天 </span></span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">while</span>(i&lt;month<span class="number">-1</span>)&#123; <span class="comment">//计算该月之前有多少天</span></span><br><span class="line">			days_sum += months[i];</span><br><span class="line">			i++;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">//然后该月第几天加上去</span></span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;该日期是第&quot;</span> &lt;&lt; days_sum+day &lt;&lt; <span class="string">&quot;天&quot;</span> &lt;&lt;endl; </span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> year, month, day;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;输入年份：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	cin &gt;&gt; year;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;输入月：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	cin &gt;&gt; month;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;输入日：&quot;</span> &lt;&lt; endl;</span><br><span class="line">	cin &gt;&gt; day;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//可以添加判断日期是否合法的函数... 现假设合法 正确输入格式 </span></span><br><span class="line">	</span><br><span class="line">	<span class="built_in">fun</span>(year, month, day);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></div></figure>
</div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/blog/2021/03/11/C_C++%E5%8A%9B%E6%89%A3LeetCode%E8%BD%AC%E8%BD%BD/">C/C++力扣LeetCode转载</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-11</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-03-24</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">7.1k</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">53分</span></span></div></header><div class="post-body"><div class="post-excerpt"><!-- 页面必备样式 -->
<style>
.back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;}
.back-top>a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height: 50px;border-radius: 50px;background-color:rgba(255,255,255,0.5);}
</style>
<div class="back-top">
    <a href="#top"> >返回顶部< </a>
</div>
<!-- toc -->
<!-- 必备样式结束 -->


        <h2 id="27-原地移出某数后的数组"   >
          <a href="#27-原地移出某数后的数组" class="heading-link"><i class="fas fa-link"></i></a><a href="#27-原地移出某数后的数组" class="headerlink" title="27.原地移出某数后的数组"></a>27.原地移出某数后的数组</h2>
      <p><strong>给你一个数组 nums 和一个值 val</strong>，你<strong>需要 原地 移除所有数值等于 val 的元素</strong>，<strong>并返回移除后数组的新长度</strong>。不要使用额外的数组空间，你必须仅使用 O(1) 额外空间并 原地 修改输入数组。元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。</p>
<p>说明:<br>为什么返回数值是整数，但输出的答案是数组呢?<br>请注意，输入数组是以「引用」方式传递的，这意味着在函数里修改输入数组对于调用者是可见的。<br>你可以想象内部操作如下:</p>
<p>// nums 是以“引用”方式传递的。也就是说，不对实参作任何拷贝<br>int len = removeElement(nums, val);</p>
<p>// 在函数里修改输入数组对于调用者是可见的。<br>// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。<br>for (int i = 0; i &lt; len; i++) {<br>    print(nums[i]);<br>}</p>
<p>示例 1：</p>
<p>输入：nums = [3,2,2,3], val = 3<br>输出：2, nums = [2,2]<br>解释：<strong>函数应该返回新的长度 2</strong>, <strong>并且 nums 中的前两个元素均为 2</strong>。你不需要考虑数组中超出新长度后面的元素。例如，函数返回的新长度为 2 ，而 nums = [2,2,3,3] 或 nums = [2,2,0,0]，也会被视作正确答案。<br>示例 2：</p>
<p>输入：nums = [0,1,2,2,3,0,4,2], val = 2<br>输出：5, nums = [0,1,4,0,3]<br>解释：函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。</p>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/remove-element" >https://leetcode-cn.com/problems/remove-element</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">removeElement</span><span class="params">(vector&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="keyword">int</span> val)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(nums.<span class="built_in">size</span>() == <span class="number">0</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        <span class="comment">//一个指针指向头 一个指向尾部</span></span><br><span class="line">        <span class="keyword">int</span> i = <span class="number">0</span>, index = nums.<span class="built_in">size</span>()<span class="number">-1</span>;</span><br><span class="line">        <span class="keyword">while</span>(i &lt;= index) </span><br><span class="line">        &#123;</span><br><span class="line">			<span class="comment">//如果当前是要删除的值</span></span><br><span class="line">            <span class="keyword">if</span>(nums[i] == val)</span><br><span class="line">            &#123;</span><br><span class="line">           	 	<span class="comment">//将该数字与最后一个数字交换</span></span><br><span class="line">            	<span class="comment">//同时数组缩减</span></span><br><span class="line">                <span class="keyword">int</span> temp = nums[index];</span><br><span class="line">                nums[index] = nums[i];</span><br><span class="line">                nums[i] = temp;</span><br><span class="line">                --index;</span><br><span class="line">            &#125;</span><br><span class="line">            	<span class="comment">//如果不是要删除的值 继续往后推进</span></span><br><span class="line">            <span class="keyword">else</span></span><br><span class="line">                ++i;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> i;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>

<p>作者：Xiaohu9527<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/remove-element/solution/cyi-ci-li-bian-bu-xu-yao-bao-li-gan-jue-82cz7/" >https://leetcode-cn.com/problems/remove-element/solution/cyi-ci-li-bian-bu-xu-yao-bao-li-gan-jue-82cz7/</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>来源：力扣（LeetCode）<br>著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。</p>

        <h2 id="58-返回最后一个单词的长度"   >
          <a href="#58-返回最后一个单词的长度" class="heading-link"><i class="fas fa-link"></i></a><a href="#58-返回最后一个单词的长度" class="headerlink" title="58.返回最后一个单词的长度"></a>58.返回最后一个单词的长度</h2>
      <p><strong>给你一个字符串 s，由若干单词组成，单词之间用空格隔开。<strong><strong>返回字符串中最后一个单词的长度。</strong></strong>如果不存在</strong>最后一个单词，<strong>请返回 0</strong> 。<strong>（注意本题最后 可能出现连续的空格）</strong>单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。</p>
<p>示例 1：</p>
<p>输入：s = “Hello World”<br>输出：5<br>示例 2：</p>
<p>输入：s = “ “<br>输出：0</p>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/length-of-last-word" >https://leetcode-cn.com/problems/length-of-last-word</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
</p>

<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">lengthOfLastWord</span><span class="params">(string s)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> count = <span class="number">0</span>, end = s.<span class="built_in">size</span>()<span class="number">-1</span>;</span><br><span class="line">        <span class="comment">//直到最后一个变成第一个</span></span><br><span class="line">        <span class="comment">//未遇到第一个字母前 若有连续的空格 直接跳过</span></span><br><span class="line">        <span class="keyword">while</span>(end &gt;= <span class="number">0</span> &amp;&amp; s[end] == <span class="string">&#x27; &#x27;</span>)  </span><br><span class="line">			--end;</span><br><span class="line">		<span class="comment">//如果没有遇到最后一个字母前的空格 就count++ 且每次指针 end--</span></span><br><span class="line">        <span class="keyword">while</span>(end &gt;= <span class="number">0</span> &amp;&amp; s[end--] != <span class="string">&#x27; &#x27;</span>)</span><br><span class="line">            ++count ;</span><br><span class="line">        <span class="keyword">return</span> count;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="动态规划实现斐波那契数列"   >
          <a href="#动态规划实现斐波那契数列" class="heading-link"><i class="fas fa-link"></i></a><a href="#动态规划实现斐波那契数列" class="headerlink" title="动态规划实现斐波那契数列"></a>动态规划实现斐波那契数列</h2>
      <p>假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢？注意：给定 n 是一个正整数。</p>
<p>示例 1：<br>输入： 2<br>输出： 2<br>解释： 有两种方法可以爬到楼顶。</p>
<ol>
<li> 1 阶 + 1 阶</li>
<li> 2 阶</li>
</ol>
<p>示例 2：<br>输入： 3<br>输出： 3<br>解释： 有三种方法可以爬到楼顶。</p>
<ol>
<li> 1 阶 + 1 阶 + 1 阶</li>
<li> 1 阶 + 2 阶</li>
<li> 2 阶 + 1 阶</li>
</ol>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/climbing-stairs" >https://leetcode-cn.com/problems/climbing-stairs</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
</p>

<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//一定看懂本题的动态规划</span></span><br><span class="line"><span class="comment">//利用dp[i]将可能用到的值存起来 return dp[n]</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">climbStairs</span><span class="params">(<span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">		<span class="function">vector&lt;<span class="keyword">int</span>&gt; <span class="title">dp</span><span class="params">(n+<span class="number">3</span>, <span class="number">0</span>)</span></span>;  <span class="comment">//元素个数为n+3 默认为0</span></span><br><span class="line">		dp[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">		dp[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line">		dp[<span class="number">2</span>] = <span class="number">2</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//用数组把值存储起来 如果下次需要 直接调用相应的数组值即可 </span></span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">3</span>; i&lt;=n; i++)&#123;</span><br><span class="line">			dp[i] = dp[i<span class="number">-1</span>] + dp[i<span class="number">-2</span>];</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> dp[n];</span><br><span class="line">    &#125;</span><br><span class="line">   </span><br></pre></td></tr></table></div></figure>


        <h2 id="合并有序数组使其有序"   >
          <a href="#合并有序数组使其有序" class="heading-link"><i class="fas fa-link"></i></a><a href="#合并有序数组使其有序" class="headerlink" title="合并有序数组使其有序"></a>合并有序数组使其有序</h2>
      <p>给你两个有序整数数组 nums1 和 nums2，请你将 nums2 合并到 nums1 中，使 nums1 成为一个有序数组。 （本来都有序，想要合并之后仍有序）初始化 nums1 和 nums2 的元素数量分别为 m 和 n 。你可以假设 nums1 的空间大小等于 m + n，这样它就有足够的空间保存来自 nums2 的元素。</p>
<p>示例 1：<br>输入：nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3<br>输出：[1,2,2,3,5,6]</p>
<p>示例 2：<br>输入：nums1 = [1], m = 1, nums2 = [], n = 0<br>输出：[1]</p>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/merge-sorted-array" >https://leetcode-cn.com/problems/merge-sorted-array</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">merge</span><span class="params">(vector&lt;<span class="keyword">int</span>&gt;&amp; nums1, <span class="keyword">int</span> m, vector&lt;<span class="keyword">int</span>&gt;&amp; nums2, <span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">    	<span class="comment">//从将nums2的全部内容 排在nums1的数组后面</span></span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> k=m,j=<span class="number">0</span>; k&lt;m+n; k++,j++)&#123;</span><br><span class="line">        	nums1[k] = nums2[j];</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">//整体对nums1第一个元素 以及加进来之后的最后一个元素进行排序即可 注意排序的长度是容纳n1 n2的长度之和</span></span><br><span class="line">		<span class="built_in">sort</span>(nums1.<span class="built_in">begin</span>(), nums1.<span class="built_in">begin</span>()+(m+n));</span><br><span class="line">		 </span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="找到值的索引-或者理应插入的位置"   >
          <a href="#找到值的索引-或者理应插入的位置" class="heading-link"><i class="fas fa-link"></i></a><a href="#找到值的索引-或者理应插入的位置" class="headerlink" title="找到值的索引 或者理应插入的位置"></a>找到值的索引 或者理应插入的位置</h2>
      <p><strong>给定一个排序数组和一个目标值</strong>，在<strong>数组中找到目标值，并返回其索引</strong>。<strong>如果目标值不存在于数组中，返回它将会被按顺序插入的位置。</strong>你可以假设数组中无重复元素。</p>
<p>示例 1:</p>
<p>输入: [1,3,5,6], 5<br>输出: 2<br>示例 2:</p>
<p>输入: [1,3,5,6], 2<br>输出: 1<br>示例 3:</p>
<p>输入: [1,3,5,6], 7<br>输出: 4<br>示例 4:</p>
<p>输入: [1,3,5,6], 0<br>输出: 0</p>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/search-insert-position" >https://leetcode-cn.com/problems/search-insert-position</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">searchInsert</span><span class="params">(vector&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="keyword">int</span> target)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">int</span> pos = <span class="number">0</span>;</span><br><span class="line">		<span class="keyword">int</span> end = nums.<span class="built_in">size</span>();</span><br><span class="line">		</span><br><span class="line">        <span class="comment">//处理特殊情况 当小于第一个或者大于最后一个时</span></span><br><span class="line">		<span class="keyword">if</span>(target &lt; nums[<span class="number">0</span>])&#123;</span><br><span class="line">			<span class="keyword">return</span> pos;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">if</span>(target &gt; nums[end<span class="number">-1</span>])&#123;</span><br><span class="line">			pos = end;</span><br><span class="line">			<span class="keyword">return</span> pos;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">        <span class="comment">//如果中间有 那么返回该位置</span></span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;end; i++)&#123;</span><br><span class="line">			<span class="keyword">if</span>(target == nums[i])&#123;</span><br><span class="line">				pos = i;</span><br><span class="line">				<span class="keyword">return</span> pos;	</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//如果下一个比它大 则返回下一个位置</span></span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;end; i++)&#123;</span><br><span class="line">			<span class="keyword">if</span>(target &gt;nums[i] &amp;&amp; target &lt; nums[i+<span class="number">1</span>])&#123;</span><br><span class="line">				pos = i+<span class="number">1</span>;</span><br><span class="line">				<span class="keyword">return</span> pos;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> pos;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="121-买卖股票的最佳时机"   >
          <a href="#121-买卖股票的最佳时机" class="heading-link"><i class="fas fa-link"></i></a><a href="#121-买卖股票的最佳时机" class="headerlink" title="121. 买卖股票的最佳时机"></a>121. 买卖股票的最佳时机</h2>
      <p>给定一个数组 prices ，它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。<br>你只能选择 某一天 买入这只股票，并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润<br>返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润，返回 0 。</p>
<p><strong>示例 1：</strong><br>输入：[7,1,5,3,6,4]<br>输出：5<br>解释：在第 2 天（股票价格 = 1）的时候买入，在第 5 天（股票价格 = 6）的时候卖出，最大利润 = 6-1 = 5 。<br>     注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格；同时，你不能在买入前卖出股票。</p>
<p><strong>示例 2：</strong><br>输入：prices = [7,6,4,3,1]<br>输出：0<br>解释：在这种情况下, 没有交易完成, 所以最大利润为 0。</p>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock" >https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">maxProfit</span><span class="params">(vector&lt;<span class="keyword">int</span>&gt;&amp; prices)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">int</span> profit=<span class="number">0</span>, minValue=prices[<span class="number">0</span>]; <span class="comment">//假设第一个是最小值</span></span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>; i&lt;prices.<span class="built_in">size</span>(); i++)&#123; <span class="comment">//从第二个到最后一个开始 </span></span><br><span class="line">			minValue = <span class="built_in">min</span>(minValue, prices[i]); <span class="comment">//每次比较minValue与当前值的最小值 </span></span><br><span class="line">			profit = <span class="built_in">max</span>(profit, prices[i]-minValue); <span class="comment">//利润就是当前值减去最小值的最大差值 </span></span><br><span class="line">		&#125; </span><br><span class="line">		<span class="keyword">return</span> profit;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="202-快乐数"   >
          <a href="#202-快乐数" class="heading-link"><i class="fas fa-link"></i></a><a href="#202-快乐数" class="headerlink" title="202. 快乐数"></a>202. 快乐数</h2>
      <p>编写一个算法来判断一个数 n 是不是快乐数。<br>「快乐数」定义为：<br>对于一个正整数，每一次将该数替换为它每个位置上的数字的平方和。然后重复这个过程直到这个数变为 1，也可能是 无限循环 但始终变不到 1。<br>如果 可以变为  1，那么这个数就是快乐数。<br>如果 n 是快乐数就返回 true ；不是，则返回 false 。</p>
<p><strong>示例 1：</strong><br>输入：19<br>输出：true<br>解释：<br>12 + 92 = 82<br>82 + 22 = 68<br>62 + 82 = 100<br>12 + 02 + 02 = 1</p>
<p><strong>示例 2：</strong><br>输入：n = 2<br>输出：false</p>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/happy-number" >https://leetcode-cn.com/problems/happy-number</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="comment">//拿到相应的各数之和函数</span></span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">getNum</span><span class="params">(<span class="keyword">int</span> n)</span></span>&#123;</span><br><span class="line">		<span class="keyword">int</span> sum=<span class="number">0</span>;</span><br><span class="line">		<span class="keyword">while</span>(n)&#123;</span><br><span class="line">			sum += (<span class="built_in">pow</span>(n%<span class="number">10</span>, <span class="number">2</span>));</span><br><span class="line">			n /= <span class="number">10</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> sum;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">bool</span> <span class="title">isHappy</span><span class="params">(<span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">		set&lt;<span class="keyword">int</span>&gt; s; </span><br><span class="line">        <span class="comment">//定义一个集合 因为如果结果不为1 相加一定有重复的数字</span></span><br><span class="line">		</span><br><span class="line">		<span class="keyword">while</span>(<span class="number">1</span>)&#123;     <span class="comment">//一直循环</span></span><br><span class="line">			</span><br><span class="line">			<span class="keyword">int</span> sum = <span class="built_in">getNum</span>(n);</span><br><span class="line">            <span class="comment">//每次将结果放入集合之前 先判断集合是否已经存在该元素</span></span><br><span class="line"></span><br><span class="line">        	<span class="comment">//如果已经出现了 说明结果肯定不会等于1</span></span><br><span class="line">            <span class="keyword">if</span>(s.<span class="built_in">count</span>(sum) == <span class="number">1</span>)&#123;</span><br><span class="line">				<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">//如果没有重复 则加入</span></span><br><span class="line">			s.<span class="built_in">insert</span>(sum);</span><br><span class="line">			</span><br><span class="line">            <span class="comment">//如果结果等于1 则返回true</span></span><br><span class="line">            <span class="keyword">if</span>(sum == <span class="number">1</span>)&#123;</span><br><span class="line">				<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="comment">//对每一次得到的sum重复计算！</span></span><br><span class="line">            n = sum;	</span><br><span class="line">		&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="136-只出现一次的数字"   >
          <a href="#136-只出现一次的数字" class="heading-link"><i class="fas fa-link"></i></a><a href="#136-只出现一次的数字" class="headerlink" title="136. 只出现一次的数字"></a>136. 只出现一次的数字</h2>
      <p>给定一个非空整数数组，除了某个元素只出现一次以外，其余每个元素均出现两次。找出那个只出现了一次的元素。</p>
<p><strong>示例 1:</strong><br>输入: [2,2,1]<br>输出: 1</p>
<p><strong>示例 2:</strong><br>输入: [4,1,2,1,2]<br>输出: 4</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">singleNumber</span><span class="params">(vector&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">           set&lt;<span class="keyword">int</span>&gt; s;</span><br><span class="line">           <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">           <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;nums.<span class="built_in">size</span>(); i++)&#123;</span><br><span class="line">           		<span class="keyword">if</span>(s.<span class="built_in">count</span>(nums[i]) == <span class="number">1</span>)&#123; <span class="comment">//如果集合中有这个元素 那么删除它</span></span><br><span class="line">           			s.<span class="built_in">erase</span>(nums[i]);</span><br><span class="line">				&#125;<span class="keyword">else</span>&#123;<span class="comment">//如果集合中没有这个元素 就添加进去</span></span><br><span class="line">					s.<span class="built_in">insert</span>(nums[i]);</span><br><span class="line">				&#125;</span><br><span class="line">		   &#125;</span><br><span class="line">		   set&lt;<span class="keyword">int</span>&gt;::iterator it; </span><br><span class="line">		   <span class="keyword">for</span>(it=s.<span class="built_in">begin</span>();it != s.<span class="built_in">end</span>(); it++)&#123;</span><br><span class="line">		   		res = *it;<span class="comment">//最后剩余的元素 一定是不重复的元素</span></span><br><span class="line">		   &#125;</span><br><span class="line">           <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>

        <h2 id="204-计数质数"   >
          <a href="#204-计数质数" class="heading-link"><i class="fas fa-link"></i></a><a href="#204-计数质数" class="headerlink" title="204. 计数质数"></a>204. 计数质数</h2>
      <p>统计所有小于非负整数 n 的质数的数量。</p>
<p>示例 1：<br>输入：n = 10<br>输出：4<br>解释：小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。</p>
<p>示例 2：<br>输入：n = 0<br>输出：0</p>
<p>示例 3：<br>输入：n = 1<br>输出：0</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">isPrime</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;</span><br><span class="line">		<span class="comment">//在0~根号x的范围内 即可判断是否是质数</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">2</span>; i * i &lt;= x; ++i) &#123;  </span><br><span class="line">            <span class="keyword">if</span> (x % i == <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//在C++中 true的数字代表1 多少个true相加</span></span><br><span class="line">	<span class="comment">//就代表有多少个质数 从2~n-1判断即可</span></span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">countPrimes</span><span class="params">(<span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">2</span>; i &lt; n; ++i) &#123;</span><br><span class="line">            ans += <span class="built_in">isPrime</span>(i);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="217-存在重复元素"   >
          <a href="#217-存在重复元素" class="heading-link"><i class="fas fa-link"></i></a><a href="#217-存在重复元素" class="headerlink" title="217. 存在重复元素"></a>217. 存在重复元素</h2>
      <p>给定一个整数数组，判断是否存在重复元素。</p>
<p>如果存在一值在数组中出现至少两次，函数返回 true 。如果数组中每个元素都不相同，则返回 false 。</p>
<p>示例 1:<br>输入: [1,2,3,1]<br>输出: true</p>
<p>示例 2:<br>输入: [1,2,3,4]<br>输出: false</p>
<p>示例 3:<br>输入: [1,1,1,3,3,4,3,2,4,2]<br>输出: true</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">containsDuplicate</span><span class="params">(vector&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">		set&lt;<span class="keyword">int</span>&gt; s;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;nums.<span class="built_in">size</span>(); i++)&#123;</span><br><span class="line">			</span><br><span class="line">			<span class="comment">//如果集合中存在重复元素 直接return真</span></span><br><span class="line">			<span class="keyword">if</span>(s.<span class="built_in">count</span>(nums[i])  == <span class="number">1</span>)&#123; </span><br><span class="line">				<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">			<span class="comment">//否则持续向集合中 添加数组中的元素</span></span><br><span class="line">			s.<span class="built_in">insert</span>(nums[i]);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="258-各位相加"   >
          <a href="#258-各位相加" class="heading-link"><i class="fas fa-link"></i></a><a href="#258-各位相加" class="headerlink" title="258. 各位相加"></a>258. 各位相加</h2>
      <p>给定一个非负整数 num，反复将各个位上的数字相加，直到结果为一位数。</p>
<p>示例:<br>输入: 38<br>输出: 2<br>解释: 各位相加的过程为：3 + 8 = 11, 1 + 1 = 2。 由于 2 是一位数，所以返回 2。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="comment">//计算各数之和的方法</span></span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">getNum</span><span class="params">(<span class="keyword">int</span> num)</span></span>&#123;</span><br><span class="line">		<span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">		<span class="keyword">while</span>(num)&#123;</span><br><span class="line">			res += num%<span class="number">10</span>;</span><br><span class="line">			num /= <span class="number">10</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> res;	</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">int</span> <span class="title">addDigits</span><span class="params">(<span class="keyword">int</span> num)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">		<span class="comment">//不到跳出循环的条件不罢休</span></span><br><span class="line">		<span class="keyword">while</span>(<span class="number">1</span>)&#123;</span><br><span class="line">			<span class="comment">//算出num的各数之和</span></span><br><span class="line">			res = <span class="built_in">getNum</span>(num); </span><br><span class="line">			<span class="comment">//直到算到个数之和小于10</span></span><br><span class="line">			<span class="keyword">if</span>(res&lt;<span class="number">10</span>)&#123;</span><br><span class="line">				<span class="keyword">return</span> res;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="comment">//下一次计算新结果的个数之和</span></span><br><span class="line">			num = res;</span><br><span class="line">		&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="28-实现-strStr"   >
          <a href="#28-实现-strStr" class="heading-link"><i class="fas fa-link"></i></a><a href="#28-实现-strStr" class="headerlink" title="28.实现 strStr()"></a>28.实现 strStr()</h2>
      <p>给定一个 haystack 字符串和一个 needle 字符串，在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在，则返回  -1。</p>
<p><strong>示例 1:</strong><br>输入: haystack = “hello”, needle = “ll”<br>输出: 2</p>
<p><strong>示例 2:</strong><br>输入: haystack = “aaaaa”, needle = “bba”<br>输出: -1</p>
<p>当 needle 是空字符串时，我们应当返回什么值呢？这是一个在面试中很好的问题。<br>对于本题而言，当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。</p>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/implement-strstr" >https://leetcode-cn.com/problems/implement-strstr</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">strStr</span><span class="params">(string haystack, string needle)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> pos = <span class="number">0</span>;</span><br><span class="line">		<span class="comment">//如果是空字符串 返回0</span></span><br><span class="line">        <span class="keyword">if</span>(needle == <span class="string">&quot; &quot;</span>)&#123; </span><br><span class="line">            <span class="keyword">return</span> pos;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			<span class="comment">//如果不为空 则找到对应的位置</span></span><br><span class="line">            pos = haystack.<span class="built_in">find</span>(needle);</span><br><span class="line">			<span class="comment">//如果找得到 返回位置</span></span><br><span class="line">            <span class="keyword">if</span>( pos != string::npos)&#123;</span><br><span class="line">                <span class="keyword">return</span> pos;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">		<span class="comment">//如果找不到 就返回-1</span></span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="83-删除排序链表中的重复元素"   >
          <a href="#83-删除排序链表中的重复元素" class="heading-link"><i class="fas fa-link"></i></a><a href="#83-删除排序链表中的重复元素" class="headerlink" title="83. 删除排序链表中的重复元素"></a>83. 删除排序链表中的重复元素</h2>
      <p>给定一个排序链表，删除所有重复的元素，使得每个元素只出现一次。</p>
<p><strong>示例 1:</strong><br>输入: 1-&gt;1-&gt;2<br>输出: 1-&gt;2</p>
<p><strong>示例 2:</strong><br>输入: 1-&gt;1-&gt;2-&gt;3-&gt;3<br>输出: 1-&gt;2-&gt;3</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Definition for singly-linked list.</span></span><br><span class="line"><span class="comment"> * struct ListNode &#123;</span></span><br><span class="line"><span class="comment"> *     int val;</span></span><br><span class="line"><span class="comment"> *     ListNode *next;</span></span><br><span class="line"><span class="comment"> *     ListNode() : val(0), next(nullptr) &#123;&#125;</span></span><br><span class="line"><span class="comment"> *     ListNode(int x) : val(x), next(nullptr) &#123;&#125;</span></span><br><span class="line"><span class="comment"> *     ListNode(int x, ListNode *next) : val(x), next(next) &#123;&#125;</span></span><br><span class="line"><span class="comment"> * &#125;;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function">ListNode* <span class="title">deleteDuplicates</span><span class="params">(ListNode* head)</span> </span>&#123;</span><br><span class="line">		ListNode* cur = head;</span><br><span class="line">		<span class="keyword">while</span>(cur != <span class="literal">NULL</span> &amp;&amp; cur-&gt;next != <span class="literal">NULL</span>)&#123;</span><br><span class="line">			<span class="keyword">if</span>(cur-&gt;val == cur-&gt;next-&gt;val)&#123;</span><br><span class="line">				cur-&gt;next = cur-&gt;next-&gt;next;</span><br><span class="line">			&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">				cur = cur-&gt;next;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> head;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="344-反转字符串"   >
          <a href="#344-反转字符串" class="heading-link"><i class="fas fa-link"></i></a><a href="#344-反转字符串" class="headerlink" title="344. 反转字符串"></a>344. 反转字符串</h2>
      <p>编写一个函数，其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。</p>
<p>不要给另外的数组分配额外的空间，你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。</p>
<p>你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。</p>
<p><strong>示例 1：</strong><br>输入：[“h”,”e”,”l”,”l”,”o”]<br>输出：[“o”,”l”,”l”,”e”,”h”]</p>
<p><strong>示例 2：</strong><br>输入：[“H”,”a”,”n”,”n”,”a”,”h”]<br>输出：[“h”,”a”,”n”,”n”,”a”,”H”]</p>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/reverse-string" >https://leetcode-cn.com/problems/reverse-string</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">reverseString</span><span class="params">(vector&lt;<span class="keyword">char</span>&gt;&amp; s)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//给俩指针 一个指针指向第一个元素</span></span><br><span class="line">		<span class="comment">//再给一个指针给最后一个元素</span></span><br><span class="line">		<span class="keyword">int</span> i = <span class="number">0</span>; </span><br><span class="line">        <span class="keyword">int</span> end = s.<span class="built_in">size</span>()<span class="number">-1</span>;</span><br><span class="line">		<span class="comment">//直到俩指针相遇</span></span><br><span class="line">        <span class="keyword">while</span>(i&lt;=end)&#123;</span><br><span class="line">			<span class="comment">//交换首尾元素</span></span><br><span class="line">            <span class="keyword">char</span> temp = s[i];</span><br><span class="line">            s[i] = s[end];</span><br><span class="line">            s[end] = temp;</span><br><span class="line">			</span><br><span class="line">            i++;</span><br><span class="line">            end--;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="383-赎金信"   >
          <a href="#383-赎金信" class="heading-link"><i class="fas fa-link"></i></a><a href="#383-赎金信" class="headerlink" title="383. 赎金信"></a>383. 赎金信</h2>
      <p>给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串，判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成。<br>如果可以构成，返回 true ；否则返回 false。<br>(题目说明：为了不暴露赎金信字迹，要从杂志上搜索各个需要的字母，组成单词来表达意思。杂志字符串中的每个字符只能在赎金信字符串中使用一次。)</p>
<p><strong>示例 1：</strong><br>输入：ransomNote = “a”, magazine = “b”<br>输出：false</p>
<p><strong>示例 2：</strong><br>输入：ransomNote = “aa”, magazine = “ab”<br>输出：false</p>
<p><strong>示例 3：</strong><br>输入：ransomNote = “aa”, magazine = “aab”<br>输出：true</p>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/ransom-note" >https://leetcode-cn.com/problems/ransom-note</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">canConstruct</span><span class="params">(string ransomNote, string magazine)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">int</span> count = <span class="number">0</span>; <span class="comment">//count用来标记r串中 可以找到的个数 </span></span><br><span class="line">		<span class="keyword">int</span> r_i = <span class="number">0</span>;</span><br><span class="line">		<span class="keyword">int</span> r_len = ransomNote.<span class="built_in">size</span>();</span><br><span class="line">		<span class="keyword">int</span> m_end = magazine.<span class="built_in">size</span>();</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//从ran字符串中 一个一个判断</span></span><br><span class="line">		<span class="keyword">while</span>(r_i &lt; r_len)&#123;</span><br><span class="line">			<span class="keyword">int</span> pos = magazine.<span class="built_in">find</span>(ransomNote[r_i]);</span><br><span class="line">			<span class="keyword">if</span>( pos != string::npos)&#123;</span><br><span class="line">				count++;</span><br><span class="line">				</span><br><span class="line">				<span class="comment">//从mag中原地删除字符 </span></span><br><span class="line">				<span class="keyword">int</span> temp = magazine[pos];</span><br><span class="line">				magazine[pos] = magazine[m_end<span class="number">-1</span>];</span><br><span class="line">				magazine[m_end<span class="number">-1</span>] = temp;</span><br><span class="line">				</span><br><span class="line">				<span class="comment">//删除最后一个字符</span></span><br><span class="line">				magazine.<span class="built_in">erase</span>(m_end<span class="number">-1</span>);</span><br><span class="line">			&#125;</span><br><span class="line">			r_i++;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">//如果都可以找到 就代表true </span></span><br><span class="line">		<span class="keyword">if</span>(count == ransomNote.<span class="built_in">size</span>())&#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="387-字符串中的第一个唯一字符"   >
          <a href="#387-字符串中的第一个唯一字符" class="heading-link"><i class="fas fa-link"></i></a><a href="#387-字符串中的第一个唯一字符" class="headerlink" title="387. 字符串中的第一个唯一字符"></a>387. 字符串中的第一个唯一字符</h2>
      <p>给定一个字符串，找到它的第一个不重复的字符，并返回它的索引。如果不存在，则返回 -1。</p>
<p><strong>示例：</strong><br>s = “leetcode”<br>返回 0</p>
<p>s = “loveleetcode”<br>返回 2</p>
<p><strong>提示：你可以假定该字符串只包含小写字母。</strong></p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">firstUniqChar</span><span class="params">(string s)</span> </span>&#123;</span><br><span class="line">        </span><br><span class="line">        unordered_map&lt;<span class="keyword">char</span>, <span class="keyword">int</span>&gt; frequency;</span><br><span class="line">        </span><br><span class="line">		<span class="comment">//将字符串中的每个字符 遇到一个自增</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">char</span> ch: s) &#123;</span><br><span class="line">            ++frequency[ch];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//从头到尾 输出第一个频度为1的</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; s.<span class="built_in">size</span>(); ++i) &#123;</span><br><span class="line">            <span class="keyword">if</span> (frequency[s[i]] == <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> i;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="268-丢失的数字"   >
          <a href="#268-丢失的数字" class="heading-link"><i class="fas fa-link"></i></a><a href="#268-丢失的数字" class="headerlink" title="268. 丢失的数字"></a>268. 丢失的数字</h2>
      <p>给定一个包含 [0, n] 中 n 个数的数组 nums ，找出 [0, n] 这个范围内没有出现在数组中的那个数。</p>
<p><strong>示例 1：</strong><br>输入：nums = [3,0,1]<br>输出：2<br>解释：n = 3，因为有 3 个数字，所以所有的数字都在范围 [0,3] 内。2 是丢失的数字，因为它没有出现在 nums 中。</p>
<p><strong>示例 2：</strong><br>输入：nums = [0,1]<br>输出：2<br>解释：n = 2，因为有 2 个数字，所以所有的数字都在范围 [0,2] 内。2 是丢失的数字，因为它没有出现在 nums 中。</p>
<p><strong>示例 3：</strong><br>输入：nums = [9,6,4,2,3,5,7,0,1]<br>输出：8<br>解释：n = 9，因为有 9 个数字，所以所有的数字都在范围 [0,9] 内。8 是丢失的数字，因为它没有出现在 nums 中。</p>
<p><strong>示例 4：</strong><br>输入：nums = [0]<br>输出：1<br>解释：n = 1，因为有 1 个数字，所以所有的数字都在范围 [0,1] 内。1 是丢失的数字，因为它没有出现在 nums 中。</p>
<p><strong>提示：</strong><br>n == nums.length<br>1 &lt;= n &lt;= 104<br>0 &lt;= nums[i] &lt;= n<br>nums 中的所有数字都 独一无二</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">missingNumber</span><span class="params">(vector&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">		<span class="comment">//首先从小到大进行排序</span></span><br><span class="line">		<span class="built_in">sort</span>(nums.<span class="built_in">begin</span>(), nums.<span class="built_in">end</span>()); </span><br><span class="line">        <span class="keyword">int</span> n = nums.<span class="built_in">size</span>();</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;nums.<span class="built_in">size</span>();i++)&#123;</span><br><span class="line">			<span class="comment">//如果n在数组当中 从小到大一次判断哪个数不在数组中</span></span><br><span class="line">            <span class="keyword">if</span>(nums[i] != i)&#123;</span><br><span class="line">                <span class="keyword">return</span> i;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">		<span class="comment">//否则没有找到 那么就是n这个数</span></span><br><span class="line">        <span class="keyword">return</span> n;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="414-第三大的数"   >
          <a href="#414-第三大的数" class="heading-link"><i class="fas fa-link"></i></a><a href="#414-第三大的数" class="headerlink" title="414. 第三大的数"></a>414. 第三大的数</h2>
      <p>给你一个非空数组，返回此数组中 第三大的数 。如果不存在，则返回数组中最大的数。</p>
<p><strong>示例 1：</strong><br>输入：[3, 2, 1]<br>输出：1<br>解释：第三大的数是 1 。</p>
<p><strong>示例 2：</strong><br>输入：[1, 2]<br>输出：2<br>解释：第三大的数不存在, 所以返回最大的数 2 。</p>
<p><strong>示例 3：</strong><br>输入：[2, 2, 3, 1]<br>输出：1<br>解释：注意，要求返回第三大的数，是指在所有不同数字中排第三大的数。<br>此例中存在两个值为 2 的数，它们都排第二。在所有不同数字中排第三大的数为 1 。</p>
<p><strong>提示：</strong><br>1 &lt;= nums.length &lt;= 104<br>-231 &lt;= nums[i] &lt;= 231 - 1</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">thirdMax</span><span class="params">(vector&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">	<span class="comment">//对所给数组进行从小到大排序</span></span><br><span class="line">	<span class="built_in">sort</span>(nums.<span class="built_in">begin</span>(), nums.<span class="built_in">end</span>()); </span><br><span class="line"></span><br><span class="line">	set&lt;<span class="keyword">int</span>&gt; s;</span><br><span class="line">	<span class="keyword">int</span> len = nums.<span class="built_in">size</span>();</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;len; i++)&#123;</span><br><span class="line">		<span class="comment">//将元素加入无重复的集合中</span></span><br><span class="line">		s.<span class="built_in">insert</span>( nums[i] );</span><br><span class="line">	&#125;	</span><br><span class="line"></span><br><span class="line">	<span class="comment">//清空vector</span></span><br><span class="line">	nums.<span class="built_in">clear</span>();</span><br><span class="line">	<span class="keyword">for</span>(set&lt;<span class="keyword">int</span>&gt;::iterator it=s.<span class="built_in">begin</span>();it!=s.<span class="built_in">end</span>();it++)&#123;</span><br><span class="line">		<span class="comment">//将集合中的元素 加入数组中</span></span><br><span class="line">		nums.<span class="built_in">push_back</span>(*it);		</span><br><span class="line">	&#125; </span><br><span class="line">	</span><br><span class="line">	<span class="comment">//如果数组的长度小于3 那么返回数组的最后一个元素</span></span><br><span class="line">	<span class="keyword">if</span>(nums.<span class="built_in">size</span>()&lt;<span class="number">3</span>) <span class="keyword">return</span> nums[nums.<span class="built_in">size</span>()<span class="number">-1</span>];</span><br><span class="line"></span><br><span class="line">	<span class="comment">//否则返回倒数第三个</span></span><br><span class="line">	<span class="keyword">return</span> nums[nums.<span class="built_in">size</span>()<span class="number">-3</span>];</span><br><span class="line">&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h2 id="1004-母牛的故事"   >
          <a href="#1004-母牛的故事" class="heading-link"><i class="fas fa-link"></i></a><a href="#1004-母牛的故事" class="headerlink" title="1004: 母牛的故事"></a>1004: 母牛的故事</h2>
      <p><strong>题目描述</strong><br>有一头母牛，它每年年初生一头小母牛。每头小母牛从第四个年头开始，每年年初也生一头小母牛。请编程实现在第n年的时候，共有多少头母牛？</p>
<p><strong>输入</strong><br>输入数据由多个测试实例组成，每个测试实例占一行，包括**一个整数n(0&lt;n&lt;55)**，n的含义如题目中描述。<br>n=0表示输入数据的结束，不做处理。</p>
<p><strong>输出</strong><br>对于每个测试实例，输出在第n年的时候母牛的数量。<br>每个输出占一行。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">fun</span><span class="params">(<span class="keyword">int</span> n)</span></span>&#123;</span><br><span class="line"> 	<span class="keyword">int</span> a[N];</span><br><span class="line"> 	<span class="keyword">int</span> i;</span><br><span class="line">	<span class="comment">//避免索引错位 因此直接让a[1]=1...</span></span><br><span class="line"> 	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>; i&lt;=<span class="number">4</span>; i++)&#123;</span><br><span class="line"> 		a[i] = i;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//索引从第五年开始 状态转移方程是 ai = ai-1 + ai-3</span></span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">5</span>; i&lt;=n; i++)&#123;</span><br><span class="line">		a[i] = a[i<span class="number">-1</span>] + a[i<span class="number">-3</span>];</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//返回该数的数组位置</span></span><br><span class="line">	<span class="keyword">return</span> a[n];	</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> y = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span>(cin &gt;&gt; y)&#123;</span><br><span class="line">		<span class="keyword">if</span>(y == <span class="number">0</span>)</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		cout &lt;&lt; <span class="built_in">fun</span>(y) &lt;&lt;endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="434-字符串中的单词数"   >
          <a href="#434-字符串中的单词数" class="heading-link"><i class="fas fa-link"></i></a><a href="#434-字符串中的单词数" class="headerlink" title="434. 字符串中的单词数"></a>434. 字符串中的单词数</h2>
      <p>统计字符串中的单词个数，这里的单词指的是连续的不是空格的字符。</p>
<p>请注意，你可以假定字符串里不包括任何不可打印的字符。</p>
<p><strong>示例:</strong></p>
<p>输入: “Hello, my name is John”<br>输出: 5<br>解释: 这里的单词是指连续的不是空格的字符，所以 “Hello,” 算作 1 个单词。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">countSegments</span><span class="params">(string s)</span> </span>&#123;</span><br><span class="line">  		<span class="comment">//flag stands for if it is the Number </span></span><br><span class="line">  	    <span class="comment">//count stands for the number of Num</span></span><br><span class="line">		<span class="keyword">int</span> flag=<span class="number">0</span>,count = <span class="number">0</span>; </span><br><span class="line">		</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; s[i] != <span class="string">&#x27;\0&#x27;</span>; i++)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">if</span>(s[i] == <span class="string">&#x27; &#x27;</span>)&#123;</span><br><span class="line">				flag = <span class="number">0</span>;</span><br><span class="line">			&#125;<span class="keyword">else</span> <span class="keyword">if</span>(flag == <span class="number">0</span>)&#123;	</span><br><span class="line">                count++;</span><br><span class="line">				flag = <span class="number">1</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	  	<span class="keyword">return</span> count;</span><br><span class="line">&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="392-判断子序列"   >
          <a href="#392-判断子序列" class="heading-link"><i class="fas fa-link"></i></a><a href="#392-判断子序列" class="headerlink" title="392. 判断子序列"></a>392. 判断子序列</h2>
      <p>给定字符串 s 和 t ，判断 s 是否为 t 的子序列。<br>字符串的一个子序列是原始字符串删除一些（也可以不删除）字符而不改变剩余字符相对位置形成的新字符串。（例如，”ace”是”abcde”的一个子序列，而”aec”不是）。</p>
<p><strong>示例 1：</strong><br>输入：s = “abc”, t = “ahbgdc”<br>输出：true</p>
<p><strong>示例 2：</strong><br>输入：s = “axc”, t = “ahbgdc”<br>输出：false<br> <br><strong>提示：</strong><br>0 &lt;= s.length &lt;= 100<br>0 &lt;= t.length &lt;= 10^4<br>两个字符串都只由小写字符组成。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isSubsequence</span><span class="params">(string s, string t)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">bool</span> flag = <span class="literal">true</span>;</span><br><span class="line">	<span class="keyword">int</span> i,j = <span class="number">-1</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; s[i] != <span class="string">&#x27;\0&#x27;</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="comment">//从s中找各个字符 对应在t中的位置</span></span><br><span class="line">		j = t.<span class="built_in">find</span>(s[i]);		</span><br><span class="line">		<span class="keyword">if</span>(j != string::npos)&#123;</span><br><span class="line">			<span class="comment">//如果没找到 就返回false </span></span><br><span class="line">			<span class="comment">//否则截取找到的位置 下一个位置到结束</span></span><br><span class="line">			<span class="comment">//继续往后找</span></span><br><span class="line">			t = t.<span class="built_in">substr</span>(j+<span class="number">1</span>);</span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			flag = <span class="literal">false</span>;</span><br><span class="line">			<span class="keyword">return</span> flag;			</span><br><span class="line">		&#125;	</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> flag;</span><br><span class="line">&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="551-学生出勤记录-I"   >
          <a href="#551-学生出勤记录-I" class="heading-link"><i class="fas fa-link"></i></a><a href="#551-学生出勤记录-I" class="headerlink" title="551. 学生出勤记录 I"></a>551. 学生出勤记录 I</h2>
      <p>给定一个字符串来代表一个学生的出勤记录，这个记录仅包含以下三个字符：</p>
<p>‘A’ : Absent，缺勤<br>‘L’ : Late，迟到<br>‘P’ : Present，到场<br>如果一个学生的出勤记录中不超过一个’A’(缺勤)并且不超过两个连续的’L’(迟到),那么这个学生会被奖赏。</p>
<p>你需要根据这个学生的出勤记录判断他是否会被奖赏。</p>
<p><strong>示例 1:</strong><br>输入: “PPALLP”<br>输出: True</p>
<p><strong>示例 2:</strong><br>输入: “PPALLL”<br>输出: False</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">   </span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">checkRecord</span><span class="params">(string s)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">bool</span> flag = <span class="literal">false</span>;</span><br><span class="line">	<span class="keyword">int</span> countA=<span class="number">0</span>, countL=s.<span class="built_in">find</span>(<span class="string">&quot;LLL&quot;</span>);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;s.<span class="built_in">size</span>(); i++)&#123;</span><br><span class="line">		<span class="keyword">if</span>(s[i] == <span class="string">&#x27;A&#x27;</span>)&#123;</span><br><span class="line">			<span class="comment">//对于不用连续出现的A 通过统计即可</span></span><br><span class="line">            countA++;</span><br><span class="line">        &#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//对于连续出现L 可以看成是否出现 不满足题目要求的“字串” &quot;LLL&quot;即可</span></span><br><span class="line">    <span class="keyword">if</span>(countL==string::npos &amp;&amp; countA &lt;= <span class="number">1</span>)&#123;</span><br><span class="line">        flag = <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="keyword">return</span> flag;</span><br><span class="line">&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="面试题-01-03-URL化"   >
          <a href="#面试题-01-03-URL化" class="heading-link"><i class="fas fa-link"></i></a><a href="#面试题-01-03-URL化" class="headerlink" title="面试题 01.03. URL化"></a>面试题 01.03. URL化</h2>
      <p>URL化。编写一种方法，将字符串中的空格全部替换为%20。<br>假定该字符串尾部有足够的空间存放新增字符，并且知道字符串的“真实”长度。<br>（注：用Java实现的话，请使用字符数组实现，以便直接在数组上操作。）</p>
<p><strong>示例 1：</strong><br>输入：”Mr John Smith    “, 13<br>输出：”Mr%20John%20Smith”<br> <br><strong>示例 2：</strong><br>输入：”               “, 5<br>输出：”%20%20%20%20%20”</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">   <span class="function">string <span class="title">replaceSpaces</span><span class="params">(string S, <span class="keyword">int</span> length)</span> </span>&#123;</span><br><span class="line">        string s = <span class="string">&quot;&quot;</span>; <span class="comment">//用一个新的字符串来装</span></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;length; i++)&#123;</span><br><span class="line">            <span class="keyword">if</span>(S[i] == <span class="string">&#x27; &#x27;</span>)&#123;</span><br><span class="line">                s += <span class="string">&quot;%20&quot;</span>;</span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                s.<span class="built_in">push_back</span>(S[i]);</span><br><span class="line">            &#125;         </span><br><span class="line">        &#125;</span><br><span class="line">        cout &lt;&lt; s;</span><br><span class="line">        <span class="keyword">return</span> s;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="459-重复的子字符串-巧妙"   >
          <a href="#459-重复的子字符串-巧妙" class="heading-link"><i class="fas fa-link"></i></a><a href="#459-重复的子字符串-巧妙" class="headerlink" title="459. 重复的子字符串(巧妙)"></a>459. 重复的子字符串(巧妙)</h2>
      <p>给定一个非空的字符串，判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母，并且长度不超过10000。</p>
<p><strong>示例 1:</strong><br>输入: “abab”<br>输出: True<br>解释: 可由子字符串 “ab” 重复两次构成。</p>
<p><strong>示例 2:</strong><br>输入: “aba”<br>输出: False</p>
<p><strong>示例 3:</strong><br>输入: “abcabcabcabc”<br>输出: True<br>解释: 可由子字符串 “abc” 重复四次构成。 (或者子字符串 “abcabc” 重复两次构成。)</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">repeatedSubstringPattern</span><span class="params">(string s)</span> </span>&#123;  </span><br><span class="line">		<span class="comment">//如果是可用 子串重复 的串（比如abab）</span></span><br><span class="line">		<span class="comment">//那么本身轮回一圈后 下次找一定可以在 小于这个串长度的位置里</span></span><br><span class="line">		<span class="comment">//找到自身的串首次出现的位置</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">//否则如果不是可重复的（比如ab）</span></span><br><span class="line">		<span class="comment">//本身轮回一圈后 下次再找到自己的串ab 一定不在自身的长度以内 abab</span></span><br><span class="line">		<span class="keyword">return</span> (s + s).<span class="built_in">find</span>(s, <span class="number">1</span>) != s.<span class="built_in">size</span>();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h2 id="506-相对名次"   >
          <a href="#506-相对名次" class="heading-link"><i class="fas fa-link"></i></a><a href="#506-相对名次" class="headerlink" title="506. 相对名次"></a>506. 相对名次</h2>
      <p>给出 N 名运动员的成绩，找出他们的相对名次并授予前三名对应的奖牌。<br>前三名运动员将会被分别授予 “金牌”，“银牌” 和“ 铜牌”（”Gold Medal”, “Silver Medal”, “Bronze Medal”）。<br>(注：分数越高的选手，排名越靠前。)</p>
<p>示例 1:<br>输入: [5, 4, 3, 2, 1]<br>输出: [“Gold Medal”, “Silver Medal”, “Bronze Medal”, “4”, “5”]<br>解释: 前三名运动员的成绩为前三高的，因此将会分别被授予 “金牌”，“银牌”和“铜牌” (“Gold Medal”, “Silver Medal” and “Bronze Medal”).<br>余下的两名运动员，我们只需要通过他们的成绩计算将其相对名次即可。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    </span><br><span class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">bool</span> <span class="title">cmp</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span></span>&#123;</span><br><span class="line">	<span class="keyword">return</span> a&gt;b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">vector&lt;string&gt; <span class="title">findRelativeRanks</span><span class="params">(vector&lt;<span class="keyword">int</span>&gt;&amp; score)</span> </span>&#123;</span><br><span class="line">	vector&lt;string&gt; v;</span><br><span class="line">	unordered_map&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; m;</span><br><span class="line">	<span class="keyword">int</span> num = <span class="number">1</span>; <span class="comment">//根据分数 从高（第一名）到低进行排名</span></span><br><span class="line">	string gold = <span class="string">&quot;Gold Medal&quot;</span>;</span><br><span class="line">	string silver = <span class="string">&quot;Silver Medal&quot;</span>;</span><br><span class="line">	string bronze = <span class="string">&quot;Bronze Medal&quot;</span>;</span><br><span class="line">     </span><br><span class="line">	vector&lt;<span class="keyword">int</span>&gt; score1 = score; <span class="comment">//新建一个数组 用来记录分数对应的名词</span></span><br><span class="line">	</span><br><span class="line">	<span class="built_in">sort</span>(score1.<span class="built_in">begin</span>(), score1.<span class="built_in">end</span>(), cmp); <span class="comment">//最高分的排在前面</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;score1.<span class="built_in">size</span>(); i++)&#123;</span><br><span class="line">		m[score1[i]] = num++; <span class="comment">//开始记录</span></span><br><span class="line">	&#125;</span><br><span class="line">		</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;score.<span class="built_in">size</span>(); i++)&#123;</span><br><span class="line">		<span class="keyword">int</span> n = m[score[i]]; <span class="comment">//根据自己的元素 原地取对应的排名 </span></span><br><span class="line"></span><br><span class="line">		<span class="comment">//修改前三名的string（金银铜）</span></span><br><span class="line">		<span class="keyword">if</span>(n == <span class="number">1</span>)&#123;</span><br><span class="line">			v.<span class="built_in">push_back</span>(gold);</span><br><span class="line">			<span class="keyword">continue</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(n == <span class="number">2</span>)&#123;</span><br><span class="line">			v.<span class="built_in">push_back</span>(silver);</span><br><span class="line">			<span class="keyword">continue</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(n == <span class="number">3</span>)&#123;</span><br><span class="line">			v.<span class="built_in">push_back</span>(bronze);</span><br><span class="line">			<span class="keyword">continue</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		v.<span class="built_in">push_back</span>(<span class="built_in">to_string</span>(n)); </span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(string str:v)&#123;</span><br><span class="line">		cout &lt;&lt; str &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> v; </span><br><span class="line">&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>

</div></div></article></section><nav class="paginator"><div class="paginator-inner"><a class="extend prev" rel="prev" href="/blog/page/4/"><i class="fas fa-angle-left"></i></a><a class="page-number" href="/blog/">1</a><span class="space">&hellip;</span><a class="page-number" href="/blog/page/4/">4</a><span class="page-number current">5</span></div></nav></div></div><div class="sidebar-wrap" id="sidebar-wrap"><aside class="sidebar" id="sidebar"><section class="sidebar-toc hide"></section><!-- ov = overview--><section class="sidebar-ov"><div class="sidebar-ov-author"><div class="sidebar-ov-author__avatar"><img class="sidebar-ov-author__avatar_img" src="/blog/images/avatar.jpg" alt="avatar"></div><p class="sidebar-ov-author__text">C'est la vie</p></div><div class="sidebar-ov-state"><a class="sidebar-ov-state-item sidebar-ov-state-item--posts" href="/blog/archives/"><div class="sidebar-ov-state-item__count">48</div><div class="sidebar-ov-state-item__name">归档</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--categories" href="/blog/categories/"><div class="sidebar-ov-state-item__count">19</div><div class="sidebar-ov-state-item__name">分类</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--tags" href="/blog/tags/"><div class="sidebar-ov-state-item__count">20</div><div class="sidebar-ov-state-item__name">标签</div></a></div><div class="sidebar-ov-cc"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" target="_blank" rel="noopener" data-popover="知识共享许可协议" data-popover-pos="up"><img src="/blog/images/cc-by-nc-sa.svg"></a></div></section></aside></div><div class="clearfix"></div></div></main><footer class="footer" id="footer"><div class="footer-inner"><div><span>Copyright © 2021</span><span class="footer__icon"><i class="fas fa-heart"></i></span><span>何水浪</span></div><div><span>由 <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a> 强力驱动</span><span> v5.4.0</span><span class="footer__devider">|</span><span>主题 - <a href="https://github.com/liuyib/hexo-theme-stun/" title="Stun" target="_blank" rel="noopener">Stun</a></span><span> v2.6.2</span></div></div></footer><div class="loading-bar" id="loading-bar"><div class="loading-bar__progress"></div></div><div class="back2top" id="back2top"><span class="back2top__icon"><i class="fas fa-rocket"></i></span></div></div><div class="search-mask"></div><div class="search-popup"><span class="search-close"></span><div class="search-input"><input placeholder="搜索文章（支持多关键词，请用空格分隔）"></div><div class="search-results"></div></div><script src="https://cdn.jsdelivr.net/npm/jquery@v3.4.1/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.ui.min.js"></script><script>function initSearch() {
  var isXML = true;
  var search_path = 'search.json';

  if (!search_path) {
    search_path = 'search.xml';
  } else if (/json$/i.test(search_path)) {
    isXML = false;
  }

  var path = '/blog/' + search_path;
  $.ajax({
    url: path,
    dataType: isXML ? 'xml' : 'json',
    async: true,
    success: function (res) {
      var datas = isXML ? $('entry', res).map(function () {
        // 将 XML 转为 JSON
        return {
          title: $('title', this).text(),
          content: $('content', this).text(),
          url: $('url', this).text()
        };
      }).get() : res;
      var $input = $('.search-input input');
      var $result = $('.search-results');
      // 搜索对象（标题、内容）的权重，影响显示顺序
      var WEIGHT = { title: 100, content: 1 };
      var searchPost = function () {
        var searchText = $input.val().toLowerCase().trim();
        // 根据空白字符分隔关键字
        var keywords = searchText.split(/[\s]+/);
        // 搜索结果
        var matchPosts = [];

        // 有多个关键字时，将原文字整个保存下来
        if (keywords.length > 1) {
          keywords.push(searchText);
        }
        // 防止未输入字符时搜索
        if (searchText.length > 0) {
          datas.forEach(function (data) {
            var isMatch  = false;
            // 没有标题的文章使用预设的 i18n 变量代替
            var title = (data.title && data.title.trim()) || '[ 文章无标题 ]';
            var titleLower = title && title.toLowerCase();
            // 删除 HTML 标签 和 所有空白字符
            var content = data.content && data.content.replace(/<[^>]+>/g, '');
            var contentLower = content && content.toLowerCase();
            // 删除重复的 /
            var postURL = data.url && decodeURI(data.url).replace(/\/{2,}/g, '/');
            // 标题中匹配到的关键词
            var titleHitSlice = [];
            // 内容中匹配到的关键词
            var contentHitSlice = [];

            keywords.forEach(function (keyword) {
              /**
              * 获取匹配的关键词的索引
              * @param {String} keyword 要匹配的关键字
              * @param {String} text 原文字
              * @param {Boolean} caseSensitive 是否区分大小写
              * @param {Number} weight 匹配对象的权重。权重大的优先显示
              * @return {Array}
              */
              function getIndexByword (word, text, caseSensitive, weight) {
                if (!word || !text) {
                  return [];
                };

                var startIndex = 0; // 每次匹配的开始索引
                var index = -1;     // 匹配到的索引值
                var result = [];    // 匹配结果

                if (!caseSensitive) {
                  word = word.toLowerCase();
                  text = text.toLowerCase();
                }

                while((index = text.indexOf(word, startIndex)) !== -1) {
                  var hasMatch = false;
                  // 索引位置相同的关键词，保留长度较长的
                  titleHitSlice.forEach(function (hit) {
                    if (hit.index === index && hit.word.length < word.length) {
                      hit.word = word;
                      hasMatch = true;
                    }
                  });
                  startIndex = index + word.length;
                  !hasMatch && result.push({ index: index, word: word, weight: weight });
                }
                return result;
              }
              titleHitSlice = titleHitSlice.concat(getIndexByword(keyword, titleLower, false, WEIGHT.title));
              contentHitSlice = contentHitSlice.concat(getIndexByword(keyword, contentLower, false, WEIGHT.content));
            });

            var hitTitle = titleHitSlice.length;
            var hitContent = contentHitSlice.length;

            if (hitTitle > 0 || hitContent > 0) {
              isMatch = true;
            }
            if (isMatch) {
              ;[titleHitSlice, contentHitSlice].forEach(function (hit) {
                // 按照匹配文字的索引的递增顺序排序
                hit.sort(function (left, right) {
                  return left.index - right.index;
                });
              });
              /**
              * 给文本中匹配到的关键词添加标记，从而进行高亮显示
              * @param {String} text 原文本
              * @param {Array} hitSlice 匹配项的索引信息
              * @param {Number} start 开始索引
              * @param {Number} end 结束索引
              * @return {String}
              */
              function highlightKeyword (text, hitSlice, start, end) {
                if (!text || !hitSlice || !hitSlice.length) {
                  return;
                }

                var result = '';
                var startIndex = start;
                var endIndex = end;
                hitSlice.forEach(function (hit) {
                  if (hit.index < startIndex) {
                    return;
                  }

                  var hitWordEnd = hit.index + hit.word.length;
                  result += text.slice(startIndex, hit.index);
                  result += '<b>' + text.slice(hit.index, hitWordEnd) + '</b>';
                  startIndex = hitWordEnd;
                });
                result += text.slice(startIndex, endIndex);
                return result;
              }

              var postData = {};
              // 文章总的搜索权重
              var postWeight = titleHitSlice.length * WEIGHT.title + contentHitSlice.length * WEIGHT.content;
              // 标记匹配关键词后的标题
              var postTitle = highlightKeyword(title, titleHitSlice, 0, title.length) || title;
              // 标记匹配关键词后的内容
              var postContent;
              // 显示内容的长度
              var SHOW_WORD_LENGTH = 200;
              // 命中关键词前的字符显示长度
              var SHOW_WORD_FRONT_LENGTH = 20;
              var SHOW_WORD_END_LENGTH = SHOW_WORD_LENGTH - SHOW_WORD_FRONT_LENGTH;

              // 截取匹配的第一个字符，前后共 200 个字符来显示
              if (contentHitSlice.length > 0) {
                var firstIndex = contentHitSlice[0].index;
                var start = firstIndex > SHOW_WORD_FRONT_LENGTH ? firstIndex - SHOW_WORD_FRONT_LENGTH : 0;
                var end = firstIndex + SHOW_WORD_END_LENGTH;
                postContent = highlightKeyword(content, contentHitSlice, start, end);
              } else { // 未匹配到内容，直接截取前 200 个字符来显示
                postContent = content.slice(0, SHOW_WORD_LENGTH);
              }
              postData.title = postTitle;
              postData.content = postContent;
              postData.url = postURL;
              postData.weight = postWeight;
              matchPosts.push(postData);
            }
          });
        }

        var resultInnerHtml = '';
        if (matchPosts.length) {
          // 按权重递增的顺序排序，使权重大的优先显示
          matchPosts.sort(function (left, right) {
            return right.weight - left.weight;
          });
          resultInnerHtml += '<ul>';
          matchPosts.forEach(function (post) {
            resultInnerHtml += '<li><a class="search-results-title" href="' + post.url + '">';
            resultInnerHtml += post.title;
            resultInnerHtml += '</a><div class="search-results-content">';
            resultInnerHtml += post.content;
            resultInnerHtml += '</div></li>';
          });
          resultInnerHtml += '</ul>';
        } else {
          resultInnerHtml += '<div class="search-results-none"><i class="far fa-meh"></i></div>';
        }
        $result.html(resultInnerHtml);
      };
      $input.on('input', searchPost);
      $input.on('keyup', function (e) {
        if (e.keyCode === Stun.utils.codeToKeyCode('Enter')) {
          searchPost();
        }
      });
    }
  });
}

function closeSearch () {
  $('body').css({ overflow: 'auto' });
  $('.search-popup').css({ display: 'none' });
  $('.search-mask').css({ display: 'none' });
}

window.addEventListener('DOMContentLoaded', function () {
  Stun.utils.pjaxReloadLocalSearch = function () {
    $('.header-nav-search').on('click', function (e) {
      e.stopPropagation();
      $('body').css('overflow', 'hidden');
      $('.search-popup')
        .velocity('stop')
        .velocity('transition.expandIn', {
          duration: 300,
          complete: function () {
            $('.search-popup input').focus();
          }
        });
      $('.search-mask')
        .velocity('stop')
        .velocity('transition.fadeIn', {
          duration: 300
        });

      initSearch();
    });
    $('.search-mask, .search-close').on('click', function () {
      closeSearch();
    });
    $(document).on('keydown', function (e) {
      // Escape <=> 27
      if (e.keyCode === Stun.utils.codeToKeyCode('Escape')) {
        closeSearch();
      }
    });
  };

  Stun.utils.pjaxReloadLocalSearch();
}, false);

function safeOpenUrl(url) {
  var newTab = window.open();
  newTab.opener = null;
  newTab.location = url;
}

function extSearch(engine) {
  var engines = {
    google: 'https://www.google.com/search?q=',
    bing: 'https://cn.bing.com/search?q=',
    baidu: 'https://www.baidu.com/s?ie=UTF-8&wd=',
  };
  var host = window.location.host;
  var query = $('.search-input input').val().toLowerCase().trim();
  var uri = engines[engine] + query + ' site:' + host;

  if (query) {
    safeOpenUrl(uri);
  } else {
    Stun.utils.popAlert('warning', '请输入字符');
  }
}

var assistSearchList = window.CONFIG.assistSearch;

if (Array.isArray(assistSearchList)) {
  assistSearchList.forEach(function (name) {
    document.querySelector('.search-btns-item--' + name).addEventListener('click', function () {
      extSearch(name);
    }, false);
  });
}</script><script src="/blog/js/utils.js?v=2.6.2"></script><script src="/blog/js/stun-boot.js?v=2.6.2"></script><script src="/blog/js/scroll.js?v=2.6.2"></script><script src="/blog/js/header.js?v=2.6.2"></script><script src="/blog/js/sidebar.js?v=2.6.2"></script><script type="application/json" src="/blog/search.json"></script></body></html>